Django 1.11:无法使ManyToManyField工作

时间:2017-05-17 19:02:30

标签: django django-models

我有以下型号:

class Address(models.Model):   
    address1 = models.CharField(max_length=150, null=True)
    address2 = models.CharField(max_length=150, null=True, blank=True)
    city = models.CharField(max_length=50, null=True)
    state_province = models.CharField(max_length=50, null=True)
    zipcode = models.CharField(max_length=10, null=True)
    country = models.CharField(max_length=3, default='USA', null=False)
    created_at = models.DateTimeField(db_index=True, auto_now_add=True)
    updated_at = models.DateTimeField(db_index=True, auto_now=True)

    class Meta:
        db_table = 'addresses'

和这一个......

class User(models.Model, AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(db_index=True, max_length=150, unique=True,
                              null=False)
    first_name = models.CharField(max_length=45, null=False)
    last_name = models.CharField(max_length=45, null=False)
    mobile_phone = models.CharField(max_length=12, null=True)
    profile_image = models.CharField(max_length=150, null=True)
    is_staff = models.BooleanField(db_index=True, null=False, default=False)
    is_active = models.BooleanField(
        _('active'),
        default=True,
        db_index=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )

    addresses = models.ManyToManyField(Address),


    USERNAME_FIELD = 'email'
    objects = MyCustomUserManager()

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

    class Meta:
        db_table = 'users'

我的第一个谜团是,通过迁移模型,没有"地址" users表中的字段,也不是数据库中的数据透视表以保持多个关系。 ManyToMany有效负载是如何保留的?

其次,我的目标是为用户提供多个地址。我希望用户拥有多个"地址" (而不是每个地址都有一个用户),因为其他模型也可以有地址。我不希望地址模型有12个不同的"所有者"与ForeignKeys一起使用的字段。

因此。我试试这个:

from myApp.models import User
from myApp.models import Address
user = User(email="test@test.com", first_name="john", last_name="doe", mobile_phone="444")
# the model permits partial address fields, don't worry about that.
address = Address(city="New York", zipcode="10014")

现在我尝试将address添加到user.addresses,然后我收到错误。

user.addresses.add(address)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-0337af6b1cd4> in <module>()
----> 1 user.addresses.add(address)

AttributeError: 'tuple' object has no attribute 'add'

帮助?

1 个答案:

答案 0 :(得分:3)

在定义多对多字段后,您有一个多余的逗号,将其转换为元组。当您删除此内容后,您会发现迁移将创建您的中间表,'Username="qtpworld.com" 'Password="qtp" '*****************************Login to gmail account using Static descriptive Programing ****************************** 'Launch gmail systemutil.Run "iexplore.exe","http:\\www.gmail.com" 'Assign object property value to a variable pwd pwd="Passwd" 'Wait till browser loads Browser("title:=Gmail: Email from Google").Page("title:=Gmail: Email from Google").Sync ' Enter Email id in Username Field Browser("title:=Gmail: Email from Google").Page("title:=Gmail: Email from Google").WebEdit("name:=Email").Set "qtpworld.com" 'Enter password in Passowrd Field Browser("title:=Gmail: Email from Google").Page("title:=Gmail: Email from Google").WebEdit("name:=" & pwd).Set "qtp" 'Cick on the Sign In Button Browser("title:=Gmail: Email from Google").Page("title:=Gmail: Email from Google").WebButton("name:=Sign in").Click 将起作用。