Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django model - Foreign Key as Primary Key

I have the following 2 tables

In models.py

class Foo(models.Model):

    uuid = models.CharField(_('UUID'), primary_key=True, default=uuid4)

and

class FooExt(models.Model):

    uuid = models.ForeignKey(Foo, verbose_name=_('UUID'), primary_key=True)
    time = models.DateTimeField(_('Create DateTime'), auto_now_add=True) 

Basically, I have Foo and FooExt. I want a one-to-one relation between FooExt. That's why I set FooExt's primary key to be foreign key into Foo (not sure if this is the right thing to do).

Now I add an entry into Foo. Does an entry for FooExt automatically get created? Or do I need to manually add an entry to both Foo and FooExt?

Is there anything I can do to get the "automatic" add feature? Conceptually, these 2 tables describe the same thing, but I just don't want to pollute Foo with extra information. So it'd be great if an add to Foo automatically creates a corresponding FooExt.

like image 958
user3240688 Avatar asked Dec 28 '15 21:12

user3240688


People also ask

Can a ForeignKey be a primary key in Django?

yes it works for me. thanks. just to play the devils advocate, what are the downsides of this method? yeah, you right, you can perform check: if Foo already created, then you dont create any new FooExt , you can do it with if self.pk: in save method, this answer may help.

What is ForeignKey in Django model?

To define a relationship between two models, you need to define the ForeignKey field in the model from the Many side of the relationship. In other words, ForeignKey should be placed in the Child table, referencing the Parent table.

How can create primary key in Django model?

If you'd like to specify a custom primary key, specify primary_key=True on one of your fields. If Django sees you've explicitly set Field.primary_key , it won't add the automatic id column. Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).


1 Answers

  1. If you want an OneToOne relation, then use models.OneToOneField instead of models.ForeignKey. with foreign keys you will need add unique=True in you ForeignKey:
class Foo(models.Model):
    uuid = models.CharField(_('UUID'), primary_key=True, default=uuid4)

class FooExt(models.Model):
    uuid = models.OneToOneField(Foo, verbose_name=_('UUID'), primary_key=True)
    time = models.DateTimeField(_('Create DateTime'), auto_now_add=True)
  1. No, an entry for FooExt don't get created when you create a Foo instance, you need to manually add an entry to both Foo and FooExt. think in Places and Restaurants, many places can be restaurants, but no all the places are restaurants.

  2. if you like an automatic add feature inside Foo that create a FooExt instance, then you can overload the save method inside Foo that create and save FooExt instance too, something like this:

class Foo(models.Model):
....
....
     def save(self, *args, **kwargs):
        super(Foo, self).save(*args, **kwargs)
        foo_ext = FooExt()
        foo_ext.uuid = self
        foo_ext.save()
like image 63
Yonsy Solis Avatar answered Dec 08 '22 00:12

Yonsy Solis