Last modified: Jan 10, 2023 By Alexander Williams

Django models.ManyToManyField (Examples)

Example 1


class Submission (models.Model):
    Source = models.ForeignKey(Source)
    PrivateToPublic = models.CharField(max_length=200)
    AccessionNumber = models.CharField(max_length=200)
    PublicDocumentCount = models.CharField(max_length=200)
    Timestamp = models.CharField(max_length=200)
    FilingDate = models.CharField(max_length=200)
    Type = models.CharField(max_length=200)
    Deletion = models.CharField(max_length=200)
    Correction = models.CharField(max_length=200)
    DateOfFilingDateChange = models.CharField(max_length=200)
    Period = models.CharField(max_length=200)
    EffectivenessDate = models.CharField(max_length=200)
    ConfirmingCopy = models.CharField(max_length=200)
    Paper = models.CharField(max_length=200)
    ActionDate = models.CharField(max_length=200)
    ReceivedDate = models.CharField(max_length=200)

class FiledFor (models.Model):
    Submission = models.ManyToManyField(Submission)
    pass

Example 2


class ReportingOwner (models.Model):
    Submission = models.ManyToManyField(Submission)
    FilingValues = models.ManyToManyField(FilingValues)
    MailAddress = models.ManyToManyField(MailAddress)
    BusinessAddress = models.ManyToManyField(BusinessAddress)
    pass

class OwnerData (models.Model):
    ReportingOwner = models.ManyToManyField(ReportingOwner)
    Cik = models.CharField(max_length=200)
    ConformedName = models.CharField(max_length=200)
    StateOfIncorporation = models.CharField(max_length=200)
    FiscalYearEnd = models.CharField(max_length=200)
    AssignedSic = models.CharField(max_length=200)
    pass

Example 3


class ActivityStream(models.Model):
    '''
    Model used to describe activity stream (audit) events
    '''

    class Meta:
        app_label = 'main'
        ordering = ('pk',)

    OPERATION_CHOICES = [
        ('create', _('Entity Created')),
        ('update', _("Entity Updated")),
        ('delete', _("Entity Deleted")),
        ('associate', _("Entity Associated with another Entity")),
        ('disassociate', _("Entity was Disassociated with another Entity"))
    ]

    actor = models.ForeignKey('auth.User', null=True, on_delete=models.SET_NULL, related_name='activity_stream')
    operation = models.CharField(max_length=13, choices=OPERATION_CHOICES)
    timestamp = models.DateTimeField(auto_now_add=True)
    changes = models.TextField(blank=True)
    deleted_actor = JSONField(null=True)

    object_relationship_type = models.TextField(blank=True)
    object1 = models.TextField()
    object2 = models.TextField()

    user = models.ManyToManyField("auth.User", blank=True)

Example 4


class Task(models.Model):
    Question_id = models.IntegerField(default=-1)
    title = models.CharField(max_length=100)
    text = models.TextField()
    timeout = models.FloatField(default=3.0)
    user_set = models.ManyToManyField(User)

    def __str__(self):
        return self.title

Example 5


class student(models.Model):        #Base Class Student which contains all details about student 1
    name=models.CharField(max_length=30)
    email=models.EmailField(max_length=30,null=True)
    id1=models.CharField(max_length=10,help_text="This is LDAP id")
    password=models.CharField(max_length=30,help_text="This is LDAP password")
    reg_done=models.IntegerField(default=0,choices=REG_DONE)
    semester=models.PositiveIntegerField(default='1st',choices=SEMESTER)
    branch=models.CharField(max_length=30,null=True,choices=COURSE)     
    mess_dues=models.PositiveIntegerField(default=0)
    mess_fees=models.PositiveIntegerField(default=0)
    lib_dues=models.PositiveIntegerField(default=0)
    reg_fees=models.PositiveIntegerField(default=0)
    def __str__(self):
        a=self.name
        return a


class course(models.Model): #Stores Course
    name=models.CharField(max_length=30)
    code=models.CharField(max_length=10,help_text="Course Code. \nExample : CS221")
    students=models.ManyToManyField(student,blank=True)
    faculty_code=models.CharField(max_length=10,null=True)      
    faculty_name=models.CharField(max_length=30)
    note=models.CharField(max_length=100,null=True)
    ltp=models.CharField(max_length=10,null=True,help_text="Enter in the form L-T-P [credit]     Example: 3-0-3 [4]\n")
    def __str__(self):
        return self.name        

Example 6


class student(models.Model):        #Base Class Student which contains all details about student 1
    name=models.CharField(max_length=30)
    email=models.EmailField(max_length=30,null=True)
    id1=models.CharField(max_length=10,help_text="This is LDAP id")
    password=models.CharField(max_length=30,help_text="This is LDAP password")
    reg_done=models.IntegerField(default=0,choices=REG_DONE)
    semester=models.PositiveIntegerField(default='1st',choices=SEMESTER)
    branch=models.CharField(max_length=30,null=True,choices=COURSE)     
    mess_dues=models.PositiveIntegerField(default=0)
    mess_fees=models.PositiveIntegerField(default=0)
    lib_dues=models.PositiveIntegerField(default=0)
    reg_fees=models.PositiveIntegerField(default=0)
    def __str__(self):
        a=self.name
        return a

    
class faculty(models.Model):    #Faculty Database
    name=models.CharField(max_length=30)
    mentor=models.BooleanField(default=False)
    id1=models.CharField(max_length=10)
    password=models.CharField(max_length=30)
    courses=models.ManyToManyField(course,blank=True)
    requests=models.ManyToManyField(request1,blank=True)
    fulldetails=models.ManyToManyField(fulldetail,blank=True)
    def __str__(self):
        return self.name