【问题标题】:Explanation of Django Table Relations and How they are Being CalledDjango 表关系的解释以及它们是如何被调用的
【发布时间】:2018-08-04 04:35:02
【问题描述】:

我对 Django 还很陌生,目前正在尝试浏览和修改一个预先存在的网站。我试图了解基于某些参数创建图表所涉及的语法。目前,该网站将账户类型作为参数,并给出账户价值与年份的关系图。如第一张图所示: Picture of Plot

我感到困惑的部分代码在这里:

tag_ids = [56, 123, 15, 21, 82]
if categories_1:
    tag_ids = categories_1.split(',')

tags = []


funding_announced_min = minyear+'-01-01'
funding_announced_max = maxyear+'-12-31'

business_list = []
for d in tag_ids:
    tag1 = Tag_objs.objects.get(pk=int(d))
    tags.append(tag1)
    business_1 = tag1.buisiness.filter(
        company__founded_date__gte=funding_announced_min,
        company__founded_date__lte=funding_announced_max,
        entity_type=0,
        )
    business_list.append(business1)

tag 是一个账户类型表,我的困惑来自 business_1 = tag1.business.filter 部分。这究竟是在做什么?

我有一个名为 biz 的模型,如下所示:

class biz(Ent):  
founded_date = models.DateField(blank=True, null=True)
employee_amount = models.IntegerField('Number of Employees',blank=True, null=True)
employee_amount_range = models.CharField(max_length=64, blank=True, null=True)
biz_status = models.CharField(choices=BIZ_STATUS_CHOICES, max_length=32, default='operating', blank=True, null=True)
zipcode = models.CharField(max_length=64, blank=True, null=True)
address = models.CharField(max_length=255, blank=True, null=True)
funding_rounds = models.IntegerField(default=0, blank=True, null=True)
funding_total_usd = models.BigIntegerField(default=0, blank=True, null=True)
first_funding_on = models.DateField(blank=True, null=True)
last_funding_on = models.DateField(blank=True, null=True)
closed_on = models.DateField(blank=True, null=True)
employee_count_min = models.IntegerField(default=0, blank=True, null=True)
employee_count_max = models.IntegerField(default=0, blank=True, null=True)

def __str__(self):  
    return self.name

class Meta:
    verbose_name_plural = "business"

但我可以看到business 被模型引用的唯一实例是verbose_name_pluralverbose_name_plural 是否意味着我可以将表 biz 称为“业务”? ent 类在这里:

class Ent(models.Model):
name = models.CharField('Name', max_length=255, db_index=True) 
desc = models.TextField('Description')
image = models.ImageField('Logo Image', upload_to='biz', blank=True, null=True, default='biz/default-user110.jpg')
cover_image = models.ImageField('Cover Image', upload_to='biz', blank=True, null=True, help_text='1300 width by 300 height top cover image.')
tags = models.ManyToManyField(Tag,related_name='business', blank=True)
featured = models.BooleanField()
limited = models.BooleanField()
city = models.CharField(max_length=128) 
state_province = models.CharField(max_length=128, blank=True, null=True) 
region = models.CharField(max_length=128, blank=True, null=True) 
country = models.CharField(choices=COUNTRY_CHOICES, max_length=128)
entity_type = models.IntegerField(choices=ENTITY_TYPE_CHOICES)
elevator_pitch = models.CharField(max_length=64, blank=True, null=True)
logo_url = models.URLField(max_length=255, blank=True, null=True)
profile_image_url = models.URLField(max_length=255, blank=True, null=True)
primary_role = models.CharField(max_length=32, blank=True, null=True)
uuid = models.CharField(max_length=128, blank=True, null=True, unique=True)
domain = models.CharField(max_length=255, blank=True, null=True)
slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)

def __str__(self):  
    return self.name

def get_absolute_url(self):

    try:
        int(self.slug[-1:])
        if self.slug.replace('-', ' ').lower() == self.name.lower():
            return reverse('business:biz', kwargs={'slug':slugify(self.name)})
        self.slug=slugify(self.name+' '+str(self.pk))
        return reverse('business:biz', kwargs={'slug':slugify(self.name+' '+str(self.pk))})
    except ValueError:
        self.slug=slugify(self.name)
        return reverse('business:biz', kwargs={'slug':slugify(self.name)})

据我了解,biz 类是ent 类的一种类型,business.filter 行正在对ent 表进行排序,该表由ent 模型创建。如果有人能给我简要解释一下有问题的线,然后表格是如何交流的,我会非常感激。如果这有点令人困惑,我深表歉意,我是新手,这是我的第一篇文章。我在之前的搜索中找不到类似的东西。

【问题讨论】:

    标签: python mysql django class models


    【解决方案1】:

    verbose_name_plural 是为管理界面设计的,而不是为 ORM 关系设计的。

    关于结构的一些高级建议:

    1. 让 biz 引用Ent 而不是从它继承。这可能会造成一些混乱。
    2. try/except 块限制在 Ent.get_absolute_url 函数中,只进行 slug 解析。
    3. 将上述方法(2)中的url解析推送到自己的clean方法中。
    4. 确保在插入之前清理Ent 上的 Pk 字段;你有 SQL 注入责任。
    5. 对于您的网址名称business:detailbusiness:biz 更清晰、更传统
    6. Ent.uuid 中的 uuid 字段应该有一个 default=uuid.uuid4 参数,以使您的生活更轻松。如果您打算只使用 postgres,还有一个内置的 django 类 - https://docs.djangoproject.com/en/2.0/ref/models/fields/#uuidfield

    【讨论】:

      【解决方案2】:

      Biz 模型继承自 Ent。 Ent 与 Tag_obj 模型是多对多的关系,并将反向访问器定义为business,这意味着您可以从一个 Tag 中使用该名称访问相关的 Biz 对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-24
        • 1970-01-01
        • 1970-01-01
        • 2011-04-01
        • 2019-03-25
        • 2012-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多