Creating Django Models


  • Python
  • Django
    • 1
    • IN
    • Verified
    • Posted byPraveen
    • TimeSept. 4, 2024, 7:21 p.m.
    • StatusActive

    Snippet Description

    Explanation: models.Model: This is the base class for all Django models. Field types: Django provides various field types to represent different kinds of data. Some common field types include: CharField: Stores a fixed-length string. TextField: Stores a long text. IntegerField: Stores an integer. BooleanField: Stores a boolean value (True or False). DateField: Stores a date. DateTimeField: Stores a date and time. FloatField: Stores a floating-point number. DecimalField: Stores a decimal number with specified precision. EmailField: Validates email addresses. URLField: Validates URLs. Relationships: Django supports various relationships between models, including: ForeignKey: Represents a one-to-many relationship. ManyToManyField: Represents a many-to-many relationship..

    Code

      from django.db import models

      class MyModel(models.Model):
          char_field = models.CharField(max_length=100)
          text_field = models.TextField()
          integer_field = models.IntegerField()
          boolean_field = models.BooleanField()
          date_field = models.DateField()
          datetime_field = models.DateTimeField()
          float_field = models.FloatField()
          decimal_field = models.DecimalField(max_digits=10, decimal_places=2)
          email_field = models.EmailField()
          url_field = models.URLField()
          foreign_key_field = models.ForeignKey('OtherModel', on_delete=models.CASCADE)
          many_to_many_field = models.ManyToManyField('OtherModel')

    Comments

    Related Posts

    Send SMS using Django-Python

    Python Django

    3 Aug. 14, 2022, 4:32 p.m.
    Proud

    Nginx 502 Bad Gateway - Django

    Nginx Python Django

    13 March 17, 2022, 3:34 p.m.
    Proud

    Django Project Updating on Server

    Nginx Python Django

    14 March 17, 2022, 2:17 p.m.
    Proud

    Want to Hire our experts?

    We'll help you to grow your career and growth.
    SignUp Today