Django的。模型字段的唯一选项不起作用

时间:2011-10-04 08:03:08

标签: django django-models django-forms

我为对象名称字段设置选项'primary_key'。之后,我通过命令'syncdb'为模型创建表。然后将'unique'选项设置为True。跑'syncdb'。但是当我使用我的对象形式时,没有任何独特性。 如果我使用添加表单,我可以指定已存在的名称,结果它将影响具有此名称的对象,并且没有任何唯一限制。 但我需要添加表单只创建对象,如果已存在具有该名称的对象,将显示唯一的限制错误。 那么“独特”选项究竟是如何运作的,我该怎样做才能以我的方式添加表单?

EDIT1:

忘了说这都是关于管理网站的。

型号:

class Rfxdigest(models.Model):
        def __unicode__(self):
            return self.crm_id
        crm_id = models.CharField(max_length=50, db_column=u'CRM_ID', primary_key=True, unique=True) # Field name made lowercase.
        Creation_date = models.DateTimeField(db_column=u'Creation_date', default=datetime.now()) # Field name made lowercase.
        Close_date = models.DateTimeField(null=True, db_column=u'Close_date', blank=True) # Field name made lowercase.
        Name = models.TextField(db_column=u'Name') # Field name made lowercase.
        StatusReasonId = models.ForeignKey(Rfxstatusreason, db_column=u'StatusReasonID') # Field name made lowercase.
        BriefDescription = models.TextField(db_column=u'Brief_description', blank=True) # Field name made lowercase.
        Notes = models.TextField(db_column=u'Notes', blank=True) # Field name made lowercase.
        POI = models.TextField(db_column=u'POI', blank=True) # Field name made lowercase.
        USP = models.TextField(db_column=u'USP', blank=True) # Field name made lowercase.
        Client_problem = models.TextField(db_column=u'Client_problem', blank=True) # Field name made lowercase.
        Currency = models.CharField(max_length=10, db_column=u'Currency', blank=True) # Field name made lowercase.
        Ballpark_begin = models.IntegerField(null=True, db_column=u'Ballpark_begin', blank=True) # Field name made lowercase.
        Ballpark_end = models.IntegerField(null=True, db_column=u'Ballpark_end', blank=True) # Field name made lowercase.
        Estimate = models.IntegerField(null=True, db_column=u'Estimate', blank=True) # Field name made lowercase.
        Ballpark_for_processing = models.IntegerField(null=True, db_column=u'Ballpark_for_processing', blank=True) # Field name made lowercase.
        Applied_efforts = models.IntegerField(null=True, db_column=u'Applied_efforts', blank=True) # Field name made lowercase.
        Coordinator = models.TextField(db_column=u'Coordinator', blank=True) # Field name made lowercase.
        Analyst = models.TextField(db_column=u'Analyst', blank=True) # Field name made lowercase.
        TechExpert = models.TextField(db_column=u'TechExpert', blank=True) # Field name made lowercase.
        SalesPerson = models.TextField(db_column=u'SalesPerson', blank=True) # Field name made lowercase.
        LeadQuality = models.CharField(max_length=10, db_column=u'LeadQuality', blank=True) # Field name made lowercase.
        Time_frame_is_realistic = models.CharField(max_length=10, db_column=u'Time_frame_is_realistic', blank=True) # Field name made lowercase.
        RFX_does_match_our_expertise_domain = models.CharField(max_length=10, db_column=u'RFX_does_match_our_expertise_domain', blank=True) # Field name made lowercase.
        Have_resources_available = models.CharField(max_length=10, db_column=u'Have_resources_available', blank=True) # Field name made lowercase.
        RFX_is_the_competitors_follow_up = models.CharField(max_length=10, db_column=u'RFX_is_the_competitors_follow_up', blank=True) # Field name made lowercase.
        RFX_is_the_follow_up_from_our_client = models.CharField(max_length=10, db_column=u'RFX_is_the_follow_up_from_our_client', blank=True) # Field name made lowercase.
        Have_competitive_edge_over_others = models.CharField(max_length=10, db_column=u'Have_competitive_edge_over_others', blank=True) # Field name made lowercase.
        Chances_of_winning_proposal_are_less_than_30_percent = models.CharField(max_length=10, db_column=u'Chances_of_winning_proposal_are_less_than_30_percent', blank=True) # Field name made lowercase.
        Customer_has_own_budget = models.CharField(max_length=10, db_column=u'Customer_has_own_budget', blank=True) # Field name made lowercase.
        RFX_has_potential_for_followup = models.CharField(max_length=10, db_column=u'RFX_has_potential_for_followup', blank=True) # Field name made lowercase.
        FP = models.CharField(max_length=10, db_column=u'FP', blank=True) # Field name made lowercase.
        TM_DT = models.CharField(max_length=10, db_column=u'TM_DT', blank=True) # Field name made lowercase.
        RFX_is_technically_interested = models.CharField(max_length=10, db_column=u'RFX_is_technically_interested', blank=True) # Field name made lowercase.
        Customer_provides_good_communication_level = models.CharField(max_length=10, db_column=u'Customer_provides_good_communication_level', blank=True) # Field name made lowercase.
        The_client_is_well_known_company = models.CharField(max_length=10, db_column=u'The_client_is_well_known_company', blank=True) # Field name made lowercase.
        Requirements_are_well_clarified = models.CharField(max_length=10, db_column=u'Requirements_are_well_clarified', blank=True) # Field name made lowercase.
        Project_may_represent_conflict_of_interests = models.CharField(max_length=10, db_column=u'Project_may_represent_conflict_of_interests', blank=True) # Field name made lowercase.
        RFX_is_the_result_of_company_campaign = models.CharField(max_length=10, db_column=u'RFX_is_the_result_of_company_campaign', blank=True) # Field name made lowercase.
        RFX_is_an_unexpected_opportunity = models.CharField(max_length=10, db_column=u'RFX_is_an_unexpected_opportunity', blank=True) # Field name made lowercase.
        RFX_is_part_of_multiproject_work = models.CharField(max_length=10, db_column=u'RFX_is_part_of_multiproject_work', blank=True) # Field name made lowercase.
        Had_conf_call_with_customer = models.CharField(max_length=10, db_column=u'Had_conf_call_with_customer', blank=True) # Field name made lowercase.
        Sent_vision = models.CharField(max_length=10, db_column=u'Sent_vision', blank=True) # Field name made lowercase.
        Sent_BP = models.CharField(max_length=10, db_column=u'Sent_BP', blank=True) # Field name made lowercase.
        Added_new_initially_unspecified_features = models.CharField(max_length=10, db_column=u'Added_new_initially_unspecified_features', blank=True) # Field name made lowercase.
        Proposed_different_platform = models.CharField(max_length=10, db_column=u'Proposed_different_platform', blank=True) # Field name made lowercase.
        class Meta:
            db_table = u'RFXDigest'
            verbose_name = 'RFX Digest'

EDIT2

刚创建新数据库并在其上运行syncdb。模型具有'unique = True'。没有改变。但在这两个数据库中,这些模型的独特性都很好:

class Rfxstatusreason(models.Model):
    def __unicode__(self):
        return self.statusname
    id = models.IntegerField(db_column=u'ID', primary_key=True) # Field name made lowercase.
    statusname = models.CharField(max_length=50, db_column=u'StatusName') # Field name made lowercase.
    class Meta:
        db_table = u'RFXStatusReason'
        verbose_name = 'RFX Status Reason'

class Rfxclosereasondescription(models.Model):
    def __unicode__(self):
        return self.closereason
    id = models.IntegerField(db_column=u'ID', primary_key=True, auto_created=True) # Field name made lowercase.
    closereason = models.CharField(max_length=50, db_column=u'CloseReason') # Field name made lowercase.
    class Meta:
        db_table = u'RFXCloseReasonDescription'
        verbose_name = 'RFX Close Reason Description'

问题是关于IntegerField和CharField吗?

2 个答案:

答案 0 :(得分:0)

syncdb不会更新表格see the docs,因为您已经创建了表格

  • 删除表格并再次运行syncdb
  • 手动更改表格

答案 1 :(得分:0)

您可以使用unique
例如:

crm_id = models.CharField(max_length = 50, unique = True, primary_key = True)

不要忘记丢弃表然后syncdb再次。