django create model object code example

Example 1: django model example

from django.db import models

class myModel(models.Model):
  input1 = models.CharField(max_length=100)
  input2 = models.TextField()
  input3 - models.DateTimeField(auto_now=True)
  
  def __str__(self):
	return self.input1

Example 2: super in django manager

# First, define the Manager subclass.
class DahlBookManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(author='Roald Dahl')

# Then hook it into the Book model explicitly.
class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)

    objects = models.Manager() # The default manager.
    dahl_objects = DahlBookManager() # The Dahl-specific manager.

Example 3: django delete object

SomeModel.objects.filter(id=id).delete()

Example 4: create django object

Author.objects.create(name="Joe")

Example 5: .save() in django

>>> one_entry = Entry.objects.get(pk=1)

Tags:

Php Example