Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python create()用相关表中的每一行填充manytomany,而不是将其留空_Python_Python 3.x_Django_Django Models - Fatal编程技术网

Python create()用相关表中的每一行填充manytomany,而不是将其留空

Python create()用相关表中的每一行填充manytomany,而不是将其留空,python,python-3.x,django,django-models,Python,Python 3.x,Django,Django Models,目前,我在models.py中有三个模型: class User(AbstractUser): pass class Watchlist(models.Model): user = models.OneToOneField(User, on_delete = models.CASCADE) listings = models.ManyToManyField('Listing', related_name = "watchlists", blank

目前,我在models.py中有三个模型:

class User(AbstractUser):
    pass 

class Watchlist(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    listings = models.ManyToManyField('Listing', related_name = "watchlists", blank = True)

class Listing(models.Model):
    title = models.CharField(max_length = 150)
    description = models.TextField()
当用户创建其观察列表时会出现问题,该列表由views.py中的以下代码处理:

# Each listing has it's own page,
# with a URL of the form /listing/<int:pk> 
def listing(request, pk):
    listing_instance = Listing.objects.get(pk = pk)
    
    if request.method == "GET":
         # return the page for the listing with primary key pk

    elif request.user.is_authenticated:   
        watchlist_instance = Watchlist.objects.get_or_create(user = request.user)[0]
        watchlist_instance.listings.add(listing_instance)

在我看来,它与中给出的示例基本相同,但与我的其他代码有相同的问题

如何确保
create()
listings
保留为空(这样我就可以将单个
listing\u实例添加到其中),而不是用表中的每个列表填充它

编辑:即使在Django的管理界面中创建观察列表,问题仍然存在;该字段将自动填充每个
列表
。此外,无法通过
clear()
remove()
或在管理界面中删除关系

Django版本3.1.1

sqlite3


Python3.8.6

OP是个白痴,这个问题从来就不存在

我假设Django的监视列表管理页面只显示相关的列表,但事实证明它只显示每个列表,无论它们是否相关

    elif request.user.is_authenticated:
        watchlist_instance = listing_instance.watchlists.create(user = request.user)