mercredi 16 décembre 2015

2 django models primary key and foreign key

I am in a need to set a model with a primary key and a foreign key. The same with a second model.

The first model has by default the tcu_id set as the primary key:

class Tcu(models.Model):
    imei = models.CharField(max_length=30, unique=True)

The second model has a primary key set to True, and a foreign key from the tcu model:

class Sim(models.Model):
    phone_num = models.CharField(max_length=30, primary_key=True)
    tcu = models.ForeignKey(Tcu, null=True, blank=True)

This is working good, but the problem comes now when I try to add a foreign key to the first model:

 class Tcu(models.Model):
        imei = models.CharField(max_length=30, unique=True)
        phone_num = models.ForeignKey(Sim, null=True, blank=True)

In tcu phone_num = models.ForeignKey(Sim) NameError: name 'Sim' is not defined

Aucun commentaire:

Enregistrer un commentaire