khancock
khancock

Reputation: 147

Django ForeignKeys not saving

Currently have a problem where I will assign a both Client foreignkey to a Signin and its not saving either reference. Heres the Signin Model

#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)

Now the employee field IS saving, but the two client_in_obj,and client_out_obj are NOT saving. I will assign them and then when refreshing the page they are not set.

Here is the client Model

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'

They client_in and client_out fields were my hackey way of trying to get by it for now.

My relations I would like to keep are as followed - An employee will have Many Sign ins, and each Sign in only needs an employee at first, each other field will be filled in over the course of a day.

Upvotes: 0

Views: 35

Answers (1)

khancock
khancock

Reputation: 147

I found the answer!

So the problem was followed. Im dealing with a legacy database in mySQL, the problem was that when django was assigning the client id it was thinking the primary key was an integer field, so when it tried assigning the key (which was a varchar) it ran into an integer field, so it didn't like it and didn't save. Hopefully that helps anyone who runs into this problem later!

Upvotes: 1

Related Questions