You need to sign in or sign up before continuing.
Commit 65e88654 authored by KUNAL GOYAL's avatar KUNAL GOYAL

models updated

parent dbb21865
from django.contrib import admin from django.contrib import admin
# Register your models here. from .models import Applicant,Choice,Application
admin.site.register(Choice)
class ChoiceInline(admin.TabularInline):
model = Application #https://stackoverflow.com/questions/5947843/django-how-does-manytomanyfield-with-through-appear-in-admin
extra = 2
class ApplicantAdmin(admin.ModelAdmin):
fields = ['name', 'rank']
inlines = [ChoiceInline]
admin.site.register(Applicant, ApplicantAdmin)
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-10-01 08:48 # Generated by Django 1.11.5 on 2017-10-01 17:09
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
...@@ -22,13 +22,30 @@ class Migration(migrations.Migration): ...@@ -22,13 +22,30 @@ class Migration(migrations.Migration):
('rank', models.IntegerField()), ('rank', models.IntegerField()),
], ],
), ),
migrations.CreateModel(
name='Application',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('priority', models.IntegerField()),
('applicant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='allocation.Applicant')),
],
),
migrations.CreateModel( migrations.CreateModel(
name='Choice', name='Choice',
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('choice_name', models.CharField(max_length=200)), ('choice_name', models.CharField(max_length=200)),
('capacity', models.IntegerField()), ('capacity', models.IntegerField()),
('applicant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='allocation.Applicant')),
], ],
), ),
migrations.AddField(
model_name='application',
name='choice',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='allocation.Choice'),
),
migrations.AddField(
model_name='applicant',
name='choices',
field=models.ManyToManyField(through='allocation.Application', to='allocation.Choice'),
),
] ]
from django.db import models from django.db import models
class Choice(models.Model):
choice_name = models.CharField(max_length=200)
capacity = models.IntegerField()
def __str__(self):
return self.choice_name
class Applicant(models.Model): class Applicant(models.Model):
choices = models.ManyToManyField(Choice, through='Application')
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
rank = models.IntegerField() rank = models.IntegerField()
def __str__(self): def __str__(self):
return self.name return self.name
def ordered_choices(self): #https://stackoverflow.com/questions/934779/django-order-a-model-by-a-many-to-many-field
"Return a properly ordered set of choices"
return self.choices.all().order_by('application__priority')
class Choice(models.Model): class Application(models.Model):
choice = models.ForeignKey(Choice, on_delete=models.CASCADE)
applicant = models.ForeignKey(Applicant, on_delete=models.CASCADE) applicant = models.ForeignKey(Applicant, on_delete=models.CASCADE)
choice_name = models.CharField(max_length=200) priority = models.IntegerField()
capacity = models.IntegerField() class Meta:
def __str__(self): # if you have an inline configured in the admin, this will
return self.choice_name # make the roles order properly
ordering = ['priority']
\ No newline at end of file
No preview for this file type
from allocation.models import Choice,Application,Applicant
a1=Applicant.objects.create(name="kunal" , rank=3)
a2=Applicant.objects.create(name="aman" , rank=59)
a3=Applicant.objects.create(name="eashan" , rank=34)
c1=Choice.objects.create(choice_name="EE",capacity=3)
c2=Choice.objects.create(choice_name="CSE",capacity=3)
c3=Choice.objects.create(choice_name="ME",capacity=3)
a1=Applicant.objects.get(id=1)
a2=Applicant.objects.get(id=2)
a3=Applicant.objects.get(id=3)
c1=Choice.objects.get(id=1)
c2=Choice.objects.get(id=2)
c3=Choice.objects.get(id=3)
m1=Application(choice=c1,applicant=a1,priority=1)
m2=Application(choice=c2,applicant=a1,priority=2)
m3=Application(choice=c3,applicant=a2,priority=1)
m4=Application(choice=c1,applicant=a3,priority=1)
m1.save()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment