Django ForeignKeys没有保存

时间:2016-08-05 16:47:59

标签: python django

目前有一个问题,我将客户端外键同时分配给Signin并且不保存任何引用。继承人登录模型

#Sign-ins require just a user
class Signin(models.Model):
    employee = models.ForeignKey(Employee)
    sign_in_time = models.DateTimeField(null=True)
    sign_out_time = models.DateTimeField(null=True)
    sign_in_number = models.CharField(max_length=15, default="00000000000")
    sign_out_number = models.CharField(max_length=15, default="00000000000")
    client_in_obj = models.ForeignKey(Clients, related_name="client_in", null=True)
    client_out_obj = models.ForeignKey(Clients, related_name="client_out", null=True)
    client_in = models.CharField(max_length=100, default="Unkown")
    client_out = models.CharField(max_length=100, null=True)
    # Start of Sign in Function
    def save(self, *args, **kwargs):
        self.client_out_obj.save()
        self.client_in_obj.save()
        super(Signin, self).save(*args, **kwargs)

    def __str__(self):
        return self.employee.first_name + " " + self.employee.last_name + " : " + str(self.sign_in_time)+ " to " + str(self.sign_out_time)

现在员工字段正在保存,但是两个client_in_obj和client_out_obj都没有保存。我将分配它们,然后在刷新页面时它们没有设置。

这是客户端模型

class Clients(models.Model):
    name = models.CharField(max_length=40, blank=True, null=True)
    alt_name = models.CharField(max_length=25, blank=True, null=True)
    address1 = models.CharField(max_length=35, blank=True, primary_key=True)
    address2 = models.CharField(max_length=35, blank=True, null=True)
    rate = models.FloatField(blank=True, null=True)
    contact_email = models.CharField(max_length=40, blank=True, null=True)
    contact_name = models.CharField(max_length=30, blank=True, null=True)
    phone = models.CharField(max_length=40, blank=True, null=True)
    billing_name = models.CharField(max_length=30, blank=True, null=True)
    billing_email_field = models.CharField(db_column='billing_email_', max_length=12, blank=True, null=True)  # Field renamed because it ended with '_'.

    def __str__(self):
        return self.name + ", " + self.address1

    class Meta:
        managed = False
        db_table = 'clients'

他们的client_in和client_out字段是我现在试图通过它的hackey方式。

我想保持的关系如下:员工将有许多登录,每次登录只需要一名员工,其他领域将在一天内填写。

1 个答案:

答案 0 :(得分:1)

我找到了答案!

所以问题就出现了。我在mySQL中处理遗留数据库,问题是当django分配客户端ID时,它认为主键是一个整数字段,所以当它尝试分配密钥(这是一个varchar)时,它会遇到一个整数字段,所以它不喜欢它,也没有保存。希望以后可以帮助任何遇到此问题的人!