如何将地址模型链接到视图

时间:2019-02-27 19:17:30

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

我正在尝试创建具有多个地址的地址表格,用户可以在其中选择家庭住址或送货地址。我有当前模型:

from django.db import models
from django.contrib.auth.models import User
from PIL import Image


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

所以我想知道那是否正确。

无论如何,我想知道如何使用当前模型创建视图,以便获得地址表格。使用普通模型会很容易,但是如何使用模型中的贯穿选项呢?

有人可以帮我吗?

谢谢

3 个答案:

答案 0 :(得分:0)

使用外键指向您的地址模型:

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.ForeignKey(Address) # <-- fix here

希望这会有所帮助!

答案 1 :(得分:0)

您应以>>>import newspaper >>>cbs_paper = newspaper.build('http://cbs.com', memoize_articles=False) >>>cbs_paper.size()1030 格式声明ForeignKey

'<app>.<model>'

或直接给班级:

class AddressType(models.Model):   
    address = models.ForeignKey('yourapp.Address', on_delete=models.CASCADE)
    profile = models.ForeignKey('yourapp.Profile', on_delete=models.CASCADE)

答案 2 :(得分:0)

其他两个答案都不正确,我最终修改了所有内容并创建了一个新模型,这里是:

Summarised_QuercusRobur1NewData<-structure(list(Stand_density_index = structure(c(1,1,2,2), .Label = c("1", 
                                                               "2"), class = "factor"), Species = structure(c(1L, 
                                                                                                                        1L, 1L, 1L), .Label = "Quercus robur", class = "factor"), Obs_no = c(9L, 
                                                                                                                                                                                             82L, 40L, 58L), Mean_Tree_Diameter = c(86.9222222222222, 121.717073170732, 
                                                                                                                                                                                                                                    82, 72.4275862068965), SD_Tree_Diameter = c(57.2766046867693, 
                                                                                                                                                                                                                                                                                134.510951231506, 60.202253131019, 61.1575440200358)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                     -4L), class = "data.frame")