如何在Django中实现地址表格?这就是我所拥有的

时间:2019-02-09 00:41:19

标签: python django django-models django-forms django-views

大家好,我试图弄清楚如何向用户添加多个地址。因此,用户可以具有送货地址和家庭住址。我有点想读一看,但是没有用。

我还创建了一个简单的架构(我忘记了邮政编码):

Screen-Shot-2019-02-08-at-7-31-36-PM.png

models.py

class Address(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60, default="Miami")
    state = models.CharField(max_length=30, default="Florida")
    zipcode = models.CharField(max_length=5, default="33165")
    country = models.CharField(max_length=50)

    class Meta:
        verbose_name = 'Address'
        verbose_name_plural = 'Address'

    def __str__(self):
        return self.name

# All user data is/should be linked to this profile, so when user gets deleted, all data deletes as well
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    nick_name = models.CharField('Nick name', max_length=30, blank=True, default='')
    bio = models.TextField(max_length=500, blank=True)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    addresses = models.ManyToManyField(
        Address,
        through='AddressType',
        through_fields=('address', 'profile'),
    )

    # If we don't have this, it's going to say profile object only
    def __str__(self):
         return f'{self.user.username} Profile'  # it's going to print username Profile

    def save(self, *args, **kwargs):
            super().save(*args, **kwargs)

            img = Image.open(self.image.path)

            if img.height > 300 or img.width > 300:
                output_size = (300, 300)
                img.thumbnail(output_size)
                img.save(self.image.path)


class AddressType(models.Model):

    HOME_ADDRESS = 1
    SHIPPING_ADDRESS = 2

    TYPE_ADDRESS_CHOICES = (
        (HOME_ADDRESS, "Home address"),
        (SHIPPING_ADDRESS, "Shipping address"),
    )

    address = models.ForeignKey('Address', on_delete=models.CASCADE)
    profile = models.ForeignKey('Profile', on_delete=models.CASCADE)

    # This is the field you would use for know the type of address.
    address_type = models.PositiveIntegerField(choices=TYPE_ADDRESS_CHOICES)


当我进行移民时,它说:

ERRORS:
users.Profile.addresses: (fields.E339) 'AddressType.address' is not a foreign key to 'Profile'.
        HINT: Did you mean one of the following foreign keys to 'Profile': profile?
users.Profile.addresses: (fields.E339) 'AddressType.profile' is not a foreign key to 'Address'.
        HINT: Did you mean one of the following foreign keys to 'Address': address?

有人可以帮我吗?

非常感谢您

1 个答案:

答案 0 :(得分:2)

首先对您的设计发表评论...

因此用户可以有许多个地址,区别在于可以是家庭住址,地址或送货地址。

您可以使用ManyToManyField并通过第三个模型“描述”该关系,该模型将提供有关送货或家庭住址的信息。

首先,我将您的“ HomeAddress”重命名为“ Address”,这样更具语义,然后使用through建立与第三张表的关系。

阅读ManyToManyFiled文档以了解更多详细信息。

示例:

class Address(models.Model):
    # ...

class Profile(models.Model):
    addresses = models.ManyToManyField(
         'Address', 
         through='AddressInfo'
         through_fields=('address', 'profile')
    )
    # ...

class AddressInfo(models.Model):

    HOME_ADDRESS = 1
    SHIPPING_ADDRESS = 2

    TYPE_ADDRESS_CHOICES = (
        (HOME_ADDRESS, "Home address"),
        (SHIPPIN_ADDRESS, "Shipping address"),
    )

    address = models.ForeignKey('Address', on_delete=models.CASCADE)
    profile = models.ForeignKey('Profile', on_delete=models.CASCADE)

    # This is the field you would use for know the type of address.
    address_type = models.PositiveIntegerField(choices=TYPE_ADDRESS_CHOICES)

关于创建表单...

然后您可以在考虑地址类型的情况下编写用于向某个配置文件添加地址的表格。

如果要同时添加多个地址,建议使用FormSetModelFormSet