ValueError:禁止save()以防止由于未保存的相关对象

时间:2015-12-21 12:19:22

标签: python mysql django

您好我正在django重新创建一个网站,这种情况发生了...... Django抛出这个错误,

ValueError:禁止使用save()来防止由于未保存的相关对象导致数据丢失' fk_deal'。

即使我已保存外键相关对象。这是视图部分。

if request.method == "POST":
    deal = DEAL()

    name = request.POST.get('name')

    revenue_2013 = request.POST.get('revenue_2013')

    if project_name:
        print project_name
        deal.name = project_name

    deal.save()

    print deal.id # prints None

    # financials 2013
    if revenue_2013:
         fin = DEALFINANCIALINFORMATION.objects.create(
                            financial_category_amount=revenue_2013,
                            financial_year='2013',
                            fk_deal = deal,
                            financial_category_id=1,
                            )

这些是模特......

class DEAL(models.Model):
    id = models.BigIntegerField(db_column='ID', primary_key=True)  # Field name made lowercase.
    company_name = models.CharField(max_length=33L, db_column=u'COMPANY_NAME', blank=True)
    investmentrequired = models.FloatField(null=True, blank=True)
    is_deleted = MySQLBooleanField(db_column=u'IS_DELETED', blank=True, default=None)
    name = models.CharField(max_length=33L, db_column=u'NAME', blank=True)
    photo = models.CharField(max_length=66L, db_column=u'PHOTO', blank=True)
    status = models.CharField(max_length=85L, db_column=u'STATUS', blank=True)
    timestamp = models.DateTimeField(null=True, db_column=u'TIMESTAMP', blank=True)
    teaser = models.TextField(db_column=u'TEASER', blank=True)
    currency_id = models.BigIntegerField(null=True, db_column=u'CURRENCY_ID', blank=True)
    description = models.TextField(db_column=u'DESCRIPTION', blank=True)
    country = models.ForeignKey('COUNTRIES', null=True, db_column=u'COUNTRY_ID', blank=True)
    sector = models.ForeignKey('SECTORS', null=True, db_column=u'SECTOR_ID', blank=True)
    type = models.ForeignKey('TYPES', null=True, db_column=u'TYPE_ID', blank=True)
    username = models.CharField(max_length=85L, db_column=u'USERNAME', blank=True)
    user = models.ForeignKey('USER', null=True, db_column=u'USER_ID', blank=True)
    hitcounter = models.BigIntegerField(null=True, db_column=u'HITCOUNTER', blank=True)
    approx_usd = models.DecimalField(decimal_places=2, null=True, max_digits=19, db_column=u'approxUSD', blank=True)
    userdeal_id = models.BigIntegerField(null=True, db_column=u'USERDEAL_ID', blank=True)
    is_featuredeal = MySQLBooleanField(db_column=u'IS_FEATUREDEAL', blank=True, default=None)
    basic_company_email = models.CharField(max_length=33L, db_column=u'BASIC_COMPANY_EMAIL', blank=True)
    basic_company_name = models.CharField(max_length=33L, db_column=u'BASIC_COMPANY_NAME', blank=True)
    basic_company_phone = models.CharField(max_length=33L, db_column=u'BASIC_COMPANY_PHONE', blank=True)
    basic_company_website = models.CharField(max_length=33L, db_column=u'BASIC_COMPANY_WEBSITE', blank=True)
    basic_elevator_pitch = models.TextField(db_column=u'BASIC_ELEVATOR_PITCH', blank=True)
    basic_premoney_evaluation = models.DecimalField(decimal_places=2, null=True, max_digits=19, db_column=u'BASIC_PREMONEY_EVALUATION', blank=True)
    basic_question_1 = models.TextField(db_column=u'BASIC_QUESTION_1', blank=True)
    basic_question_2 = models.TextField(db_column=u'BASIC_QUESTION_2', blank=True)
    basic_question_3 = models.TextField(db_column=u'BASIC_QUESTION_3', blank=True)
    basic_summary = models.TextField(db_column=u'BASIC_SUMMARY', blank=True)
    basic_total_offering_amount = models.DecimalField(decimal_places=2, null=True, max_digits=19, db_column=u'BASIC_TOTAL_OFFERING_AMOUNT', blank=True)
    is_company = MySQLBooleanField(db_column=u'IS_COMPANY', blank=True, default=None)
    is_posted = MySQLBooleanField(db_column=u'IS_POSTED', blank=True, default=None)
    is_visible = MySQLBooleanField(db_column=u'IS_VISIBLE', blank=True, default=None)
    other_company_milestones = models.TextField(db_column=u'OTHER_COMPANY_MILESTONES', blank=True)
    projectname = models.CharField(max_length=33L, db_column=u'PROJECTNAME', blank=True)
    basicdealstage = models.ForeignKey('DEALSTAGES', null=True, db_column=u'BASICDEALSTAGE_ID', blank=True)
    basic_security_type = models.ForeignKey('SECURITYTYPES', null=True, db_column=u'BasicSecurityType_ID', blank=True)
    is_public = MySQLBooleanField(db_column=u'IS_PUBLIC', blank=True, default=None)
    profile_completed = models.BigIntegerField(null=True, db_column=u'PROFILE_COMPLETED', blank=True)
    is_closed = MySQLBooleanField(db_column=u'IS_CLOSED', blank=True, default=None)
    first_step_completed = models.BigIntegerField(null=True, db_column=u'FIRST_STEP_COMPLETED', blank=True, default=0L)
    second_step_completed = models.BigIntegerField(null=True, db_column=u'SECOND_STEP_COMPLETED', blank=True)
    third_step_completed = models.BigIntegerField(null=True, db_column=u'THIRD_STEP_COMPLETED', blank=True)

    class Meta:
        db_table = u'DEAL'

    def __unicode__(self):
        if self.name:
            return self.name
        else:
            return "No name"

class DEALFINANCIALINFORMATION(models.Model):
    id = models.BigIntegerField(db_column='ID', primary_key=True)  # Field name made lowercase.
    financial_category_amount = models.DecimalField(decimal_places=2, null=True, max_digits=19, db_column=u'FINANCIAL_CATEGORY_AMOUNT', blank=True)
    financial_year = models.CharField(max_length=33L, db_column=u'FINANCIAL_YEAR', blank=True)
    de_al_id = models.BigIntegerField(null=True, db_column=u'DEAl_ID', blank=True)
    financial_category_id = models.BigIntegerField(null=True, db_column=u'FINANCIAL_CATEGORY_ID', blank=True)
    fk_deal = models.ForeignKey('DEAL', null=True, blank=True)

    class Meta:
        db_table = u'DEAL_FINANCIAL_INFORMATION'

数据库是从现有的MySQL数据库创建的.... 我使用jquery POST将数据发送到视图.... 我似乎无法找出问题所在。即使在保存模型后如果我打印了id字段,它也会返回None .... 有人可以帮帮我吗?

0 个答案:

没有答案