Python 如何在django中实现地址表单?这就是我所拥有的

Python 如何在django中实现地址表单?这就是我所拥有的,python,django,django-models,django-forms,django-views,Python,Django,Django Models,Django Forms,Django Views,大家好,我想知道如何为一个用户添加多个地址。因此,用户可以有送货地址和家庭地址。我猜是在四处阅读,但不起作用 我还创建了一个简单的模式(我忘了包括zipcode): 型号.py class Address(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60,

大家好,我想知道如何为一个用户添加多个地址。因此,用户可以有送货地址和家庭地址。我猜是在四处阅读,但不起作用

我还创建了一个简单的模式(我忘了包括zipcode):

型号.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?
谁能帮我一下吗

非常感谢您

首先请对您的设计发表评论。。。 因此,一个用户可以有多个地址,区别在于可以是家庭地址、地址或送货地址

你可以使用一个模型,通过第三个模型来“描述”关系,该模型将包含信息,无论是发货还是家庭地址

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

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

例如:

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)
关于创建表单。。。 然后,考虑地址类型,您可以编写向某个配置文件添加地址的表单


如果您想同时添加多个地址,建议使用a或a。

wow这是另一个级别的模型lol谢谢Ray,问题是,我如何在views.py中调用表单/模型?因为使用概要文件模型很容易:
instance=request.user.Profile
,但是我如何在一个新模型中做到这一点呢?谢谢你的帮助,我每天都爱着Django更不用说我想我得了它,我现在病了,所以明天我感觉好一点的时候我会给它打一针。这里解释了你说的话,明天我会给它一个快照。很高兴能帮上忙,如果你卡住了随时回来。早上好,Ray,我现在正试图这么做,但它在
through_fields=('address','profile')
行上出现了一个错误,它说SyntaxError:无效语法。我更新了上面的代码,所以你可以清楚地看到我是如何做到的:)Lo reviso hoy en la noche。