Author Picture

kim


Django Fields Unique_Together

Django Fields Unique_Together

Author Avatar wrote on 08/06/2022


class CompanyCurreny(models.Model):
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    currency = models.ForeignKey(Currency, on_delete=models.CASCADE)

    class Meta:
        unique_together = ('company', 'currency',)

    def __str__(self):
        return str(self.currency)
Read more
Django Fields Related_Name

Django Fields Related_Name

Author Avatar wrote on 08/06/2022

Related name is a must in case there 2 FKs in the model that point to the same table. For example in case of Bill of material
Read more
Upload_to File and Images

Upload_to File and Images

Author Avatar wrote on 08/06/2022

A folder

class MyModel(models.Model):
    # file will be uploaded to MEDIA_ROOT/uploads
    upload = models.FileField(upload_to='uploads/')
Read more
Data Types and Fields List

Data Types and Fields List

Author Avatar wrote on 08/06/2022

  • AutoField It An IntegerField that automatically increments.
  • BigAutoField It is a 64-bit integer, much like an AutoField except that it is guaranteed to fit numbers from 1 to 9223372036854775807.
  • BigIntegerField It is a 64-bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807.
Read more
OneToOne Relationship

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

Topics: Models