Django Hacks

 * "Hello %s, my name is %s" % ('john', 'mike') # Hello john, my name is mike".

** class Person(models.Model):

    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=20)

    def __str__(self):
        return self.id+self.name
class Companies(models.Model):
    title = models.CharField(max_length=20)
    description=models.CharField(max_length=10)
    person= models.ForeignKey(Person,related_name='persons',on_delete=models.CASCADE)
Notice the usage of on_delete=models.CASCADE in the model Companies. That is to delete all companies when the person that owns it (instance of class Person) is deleted.
*My expectation is that the on_delete=models.SET_NULL means that if a User is deleted, the Store will not be deleted.

*If you don’t specify primary_key=True for any fields in your model, Django will automatically add an IntegerField to hold the primary key, so you don’t need to set primary_key=True on any of your fields unless you want to override the default primary-key behavior. For more, see Automatic primary key fields.
* A very common example used to explain this is when you are trying build say a social media app and any register user needs to have a profile page, That particular profile is peculiar to that user in the sense that a user cannot have two profile pages at the same time meaning that one profile page to one user, hence the name OneToOne relationship. Below is a code snippet:
*OneToOne Relationship: You are in a relationship and you and your partner are not allowed to ask another person out. That is a OneToOne relationship.

ManyToOne Relationship: You are also in a relationship and only you are allowed to have MANY other partners, but your MANY other partners can only be in a relationship with you, which is probably not a good thing right?. That is a ManyToOne relationship

ManyToMany Relationship: It is a free world, you and your partner(s) are allowed to have as many other partners as possible. That is a ManyToMany relationship.

*By default, Django adds an id field to each model, which is used as the primary key for that model. You can create your own primary key field by adding the keyword arg primary_key=True to a field. If you add your own primary key field, the automatic one will not be added.

*

Comments

Popular posts from this blog

Django

JS

chat model