python - 如何在 django 中实现地址表单?这就是我所拥有的
问题描述
大家好,我正在尝试弄清楚如何向用户添加多个地址。所以用户可以有送货地址和家庭地址。我有点猜想阅读,但它不起作用。
我还创建了一个简单的模式(我忘了包括邮政编码):
模型.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?
有人可以帮帮我吗?
太感谢了
解决方案
首先对您的设计发表评论...
所以一个用户可以有很多 地址,区别在于可以是家庭地址或地址或送货地址。
您可以使用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)
关于创建表格...
然后,您可以编写表格以将地址添加到某些配置文件,同时考虑地址类型。
如果要同时添加多个地址,建议使用FormSet或ModelFormSet。
推荐阅读
- java - 持久化子表 JPA 的问题
- iis - 使用本地用户帐户的 IIS 基本身份验证允许阻止帐户
- python - 使用 pytorch Dataloader 加载并正确显示图像数据集
- node.js - 带有 AWS lambda 的 ndjson 请求
- c++ - 循环输出中的空内容
- ios - 底部布局指南和不同屏幕格式的一般方法
- c - C:循环遍历结构和连接到字符串
- docker - 如何使用 Windows 在 Internet 上公开 docker 容器
- c# - 在另一个进程完成后启动进程
- paypal - Paypal Missing Payer Address with Orders API V2 / Sandbox