Author Picture

Kim Majali


OneToOne Relationship

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)
Read more

OneToMany Relationship

Author Avatar wrote on 08/06/2022

To define a many-to-one relationship, use ForeignKey:
If a model has a ForeignKey, instances of that model will have access to the related (foreign) object via an attribute of the model.
Read more

Circular Import Problem Between Apps

Author Avatar wrote on 08/06/2022

ImportError: cannot import name 'Customer' from partially initialized module 'translation.models' (most likely due to a circular import)

To use in model fields

Instead of import from another app link to it directly App.Model
Read more

Django Model’s Methods

Author Avatar wrote on 08/06/2022

FAT models THIN view

1. Can be called from any where by selecting the object (No need to repeat)
2.Can be accessed on templates if they return value (No need to pass to the context)
Read more

Django Signals

Author Avatar wrote on 08/06/2022

Signals allow certain senders to notify a set of receivers that some action has taken place so they continue
List is here https://docs.djangoproject.com/en/4.0/ref/signals/
Read more

Topics: Models