OneToOne Relationship Django.How

Author avatar wrote on 08/06/2022

Create

class Place(models.Model):
    name = models.CharField(max_length=50)

class Restaurant(models.Model):
    place = models.OneToOneField(Place, on_delete=models.CASCADE, primary_key=True)
    serves_pizza = models.BooleanField(default=False)


Retrive

p = Restaurant.objects.get(id=1).place
# or
r = place.bjects.get(id=1).restaurant

  • https://medium.com/django-rest/one-to-one-relationships-onetoonefield-917cfd2e4ce3
  • CRUDgen Django CRUD generator