简体   繁体   English

AttributeError:模块“django.db.models”没有属性“ManyToMany”

[英]AttributeError: module 'django.db.models' has no attribute 'ManyToMany'

I am trying to make one of my model objects a ManyToMany Field, so I can access the objects through both models.我正在尝试使我的model对象之一成为ManyToMany字段,因此我可以通过这两个模型访问对象。

I am receiving the following error .我收到以下error

listing = models.ManyToMany(Listings, blank=True, related_name="listing")
AttributeError: module 'django.db.models' has no attribute 'ManyToMany'

models.py : models.py

class WatchList(models.Model):
    listing = models.ManyToMany(Listings, blank=True, related_name="listing")
    user = models.ForeignKey(User, on_delete=models.CASCADE, default="")

The name of the field is a ManyToManyField [Django-doc] , so this includes the …Field in ManyToMany Field :该字段的名称是ManyToManyField [Django-doc] ,因此这包括ManyToMany Field中的…Field Field :

from django.conf import settings

class WatchList(models.Model):
    listing = models.ManyToManyField(Listings, blank=True, related_name='watchlists')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

Note : It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly.注意:通常最好使用settings.AUTH_USER_MODEL [Django-doc]来引用用户模型,而不是直接使用User模型[Django-doc] For more information you can see the referencing the User model section of the documentation .有关更多信息,您可以查看参考文档的User模型部分


Note : The related_name=… parameter [Django-doc] is the name of the relation in reverse , so from the Listings model to the Watchlist model in this case.注意related_name=…参数[Django-doc]反向关系的名称,因此在这种情况下从Listings模型到Watchlist模型。 Therefore it (often) makes not much sense to name it the same as the forward relation.因此,将其命名为与前向关系相同的名称(通常)没有多大意义。 You thus might want to consider renaming the listing relation to watchlists .因此,您可能需要考虑将listing关系重命名为watchlists


Note : normally a Django model is given a singular name, so listing instead of listings .注意:通常 Django 模型被赋予一个单数名称,因此使用listing而不是listings

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 模块 'django.db.models' 没有属性 'Models' - AttributeError: module 'django.db.models' has no attribute 'Models' AttributeError:模块“ django.db.models”没有属性“ MultiPolygonField” - AttributeError: module 'django.db.models' has no attribute 'MultiPolygonField' AttributeError:模块 'django.db.models' 没有属性 - AttributeError: module 'django.db.models' has no attribute AttributeError:模块“django.db.models”没有属性“永久链接” - AttributeError: module 'django.db.models' has no attribute 'permalink' AttributeError: 模块 'django.db.models' 没有属性 'RESTRICT' - AttributeError: module 'django.db.models' has no attribute 'RESTRICT' AttributeError:模块“django.db.models”没有属性“RichTextField” - AttributeError: module 'django.db.models' has no attribute 'RichTextField' AttributeError: 模块 'django.db.models' 没有属性 'UniqueConstraint' - AttributeError: module 'django.db.models' has no attribute 'UniqueConstraint' AttributeError: 模块 'django.db.models' 没有属性 'BigAutoField' - AttributeError: module 'django.db.models' has no attribute 'BigAutoField' Django 属性错误:“AttributeError:模块‘django.db.models’没有属性‘ManytoManyField’” - Django Attribute Error: "AttributeError: module 'django.db.models' has no attribute 'ManytoManyField' " AttributeError:模块'django.db.models'没有属性'get_models' - AttributeError: module 'django.db.models' has no attribute 'get_models'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM