2012-06-04 118 views
0

嗨朋友,我在django模型中竞争新手。 我的问题是,我有五个表属性。Django一对多的领域..........?

Rulno (integer) 
From (IpAddressfield) 
To(IpAddressfield) 
Priority (integer) 
Cisp(CharField) 

他们可以为单一Ruleno。所以很多一些优先和CISP的,因为我喜欢写模式。

class Ruleinfo(models.Model): 
    rule = models.IntegerField(null=False) 
    From = models.IPAddressField(null=True) 
    to = models.IPAddressField(null=True) 
    priority = models.ForeignKey('Priority',related_name = 'priority1') 
    Cisp =models.ForeignKey('Priority',related_name = 'cisp1') 
    def __unicode__(self): 
     return u'%s' %(self.rule) 



class Priority(models.Model): 
    priority = models.IntegerField(null = True) 
    Ruleno = models.ForeignKey('Ruleinfo') 
    CISP = models.IntegerField(null = True) 
    def __unicode__(self): 
     return u'%s ' % (self.priority) 

我想知道,上述模型将满足我的要求或不? 。

或者让我知道是否有其他选择。

回答

6

正确的做法是在关系的另一端使用ForeignKey

+0

@lgnacio请检查更新的问题一次.. – user1409289

+1

只是一个补充:你应该降低你的模型字段的一致性和可读性。 – schneck

+0

当你在model2中有一个外键的抽象模型1,模型2时,这会中断。在为这些模型创建子类时,必须在model2_subclass中显式添加外键。这是可以接受的。但是,如果我使用抽象类编写代码,那么我无法使用model1_obj.foreignkeyfield遍历many_to_one关系,但我无法以通用方式遍历一对多字段。我不能做model2_obj.model1_field_set.all()。 –