I am trying to implement a grid layout for entering marks of students into my model. The model is described as:
class User(AbstractUser):
is_student = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=True)
email = models.EmailField('email address', blank=False)
first_name = models.CharField(max_length=50, blank=False)
last_name = models.CharField(max_length=50, blank=False)
class Student(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, related_name='student_profile')
yoe = models.IntegerField('year of enrollment', choices=YEAR_CHOICES,
default=datetime.datetime.now().year)
photo = models.ImageField(upload_to='student/%Y/')
class Paper(models.Model):
semester = models.CharField(max_length=1)
name_of_paper = models.CharField(max_length=200)
max_pass_marks = models.IntegerField("Max Marks",validators=[MaxValueValidator(99)])
class Test(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, default='')
paper = models.ForeignKey(Paper, on_delete=models.CASCADE, default='')
dot = models.DateField("Date of Test")
marks = models.IntegerField("Marks Obtained",validators=[MaxValueValidator(99)], default=0)
I want to be able to create a form with a grid layout where each row of the grid will have the students name followed by text boxes where users can enter the test scores obtained by students in each of the 'Paper' they have opted for. Obviously the test scores should then populate the 'Test' table/model. I am relatively new to django and cant really wrap my head around this. Any help will be appreciated.
Related
I have 3 models 1) university 2) courses and 3) enquiry.
My enquiry model has a foreign key to "course". And Course has a foreign key to the university. I only want to show the courses related to that university while adding an enquiry. I tried django-smart-select but failed to do so. I tried This answer but I dont understand the logic and failed to implement in my project.
this is my models.py file
class university(models.Model):
univ_name = models.CharField(max_length=100)
country = CountryField(blank=True, null=True)
univ_desc = models.CharField(max_length=1000)
univ_logo = models.ImageField(upload_to="media")
univ_phone = models.CharField(max_length=10, blank=True)
univ_email = models.EmailField(max_length=254, blank=True)
univ_website = models.URLField(blank=True)
assigned_users = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, default="")
def __str__(self):
return self.univ_name
class Meta:
verbose_name_plural = "Universities"
class Course(models.Model):
university = models.ForeignKey(university, on_delete=models.CASCADE)
course_name = models.CharField(max_length=100)
course_levels = models.ForeignKey(course_levels, on_delete=models.CASCADE)
intake = models.ForeignKey(intake, on_delete=models.CASCADE)
documents_required = models.ForeignKey(documents_required, on_delete=models.CASCADE)
course_requirements = models.ForeignKey(course_requirements, on_delete=models.CASCADE)
Active = models.BooleanField()
def __str__(self):
return self.course_name
class enquiry(models.Model):
student_name = models.CharField(max_length=100)
student_phone = models.CharField(max_length=10)
student_email = models.EmailField()
student_address = models.TextField()
current_education = models.ForeignKey(current_education, on_delete=models.CASCADE)
country_interested = CountryField(blank=True, null=True)
university_interested = models.ForeignKey(university, on_delete=models.CASCADE)
course_interested = models.ForeignKey(Course, on_delete=models.CASCADE, limit_choices_to={'Active':True})
level_applying_for = models.ForeignKey(course_levels, on_delete=models.CASCADE)
intake_interested = models.ForeignKey(intake, on_delete=models.CASCADE)
assigned_users = models.ForeignKey(User, on_delete=models.CASCADE, default="", limit_choices_to={"is_active": True})
enquiry_status = models.ForeignKey(enquiry_status, on_delete=models.CASCADE, default="")
course_interested= ChainedForeignKey(Course,chained_field= 'university_interested',chained_model_field= 'university',show_all= False,auto_choose= True,sort=True,limit_choices_to = {"Active": True},)
I want to show the course_interested field related to that university. Need help.
I tried using django-smart-select but failed to implement it. I am not aware of jquery and ajax so that is out of the question to use in my project.
got the solution I used the django-select2 and was able to solve this issue.
my models.py
class LiveClass_details(models.Model):
standard = models.ForeignKey(LiveClass, on_delete=models.CASCADE)
chapter_details = models.TextField(default='')
mentor_id = models.ForeignKey(Mentor, max_length=30, on_delete=models.CASCADE)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
doubtClass = models.OneToOneField(DoubtClasses, on_delete=models.PROTECT, null=True, blank=True)
isDraft = models.BooleanField(default=True)
ratings = models.FloatField(default=0)
no_of_students_registered = models.IntegerField(default=0)
# registered_students = models.ManyToManyField(RegisteredNames, null=True, blank=True)
no_of_students_attended = models.IntegerField(default=0)
class Meta:
verbose_name_plural = 'LiveClass_details'
class RegisteredNames(models.Model):
name = models.CharField(max_length=100, unique=True)
liveclass_id = models.ForeignKey
I am creating a endpoint where when a user register himself his name will get added to registered_students , so i had made a registered students ManyToMany Field hoping it will get updated when a user is registered but then i understand that it will contain all the names that are present in the RegisteredNames Model meaning names registered across all the liveclasses but i want only the names that are registered for a particular liveclass in the field so i need a array like field which i think is not possible so please help me in improving my logic, how can i achieve it
The documentation and django tutorials are very good: https://docs.djangoproject.com/en/3.2/topics/db/models/ https://docs.djangoproject.com/en/3.2/intro/tutorial02/#creating-models
Your code is very close. You don’t need the many-to-many field, and you need to specify the type of the Foreign key relationship in the RegisteredNames. You can do this:
class LiveClass_details(models.Model):
standard = models.ForeignKey(LiveClass, on_delete=models.CASCADE)
chapter_details = models.TextField(default='')
mentor_id = models.ForeignKey(Mentor, max_length=30, on_delete=models.CASCADE)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
doubtClass = models.OneToOneField(DoubtClasses, on_delete=models.PROTECT, null=True, blank=True)
isDraft = models.BooleanField(default=True)
ratings = models.FloatField(default=0)
no_of_students_attended = models.IntegerField(default=0)
class Meta:
verbose_name_plural = 'LiveClass_details'
class RegisteredNames(models.Model):
name = models.CharField(max_length=100, unique=True)
liveclass = models.ForeignKey(LiveClass_details, on_delete=Models.CASCADE)
Then, simply:
name = RegisteredNames.objects.create(name="Dhruv", liveclass_id=1)
To get all the registered names from a liveclass_details:
names = LiveClass_details.objects.get(id=1).registerednames_set.all()
num_reg = len(names)
I have two models, User and Card. One user has one card. I need to get objects of User if the 'card_written' of Card model is False in a view.
class User(model.Model):
phone = PhoneNumberField(null=False, blank=False, unique=True)
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
class Card(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
card_number = models.CharField(max_length=200)
card_written = models.BooleanField(default=False, null=False)
card_write_date = models.DateTimeField(null=True, blank=True)
card_delivered = models.BooleanField(default=False, null=False)
You can query on related models quite easily in Django by separating related fields by double underscores.
A query like this should work for you:
User.objects.filter(card__card_written=False)
Here card is the related name since you haven't specified it in the Card model if you want it to be something else you need to specify like so in the Card model:
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE, related_name='something_else')
I'm learning Django and I'm trying to make a sort of wiki type app. In this wiki there are different type of models: Characters, Items, Spells and Adventures. Each model has some fields that are the same (like name, author, date_created, etc.) and some that are unique to the model, like duration for adventures, alignment for characters, etc. The problem that I have is that if they are different models then every time I want to make something that is common to all of the models (like being able to like, favorite, create, edit, etc.) then I have to code for each model individually. Is there a way of creating a sort of Content model that contains every character, item, spell and adventure so that every time I want to make a form or a function I just make a Content form or Content function?
Here's some of the code, some parts are in spanish but I translated the important parts:
class Character(models.Model):
ALIGNMENT= (
('Legal Bueno', 'Legal Bueno'),
('Legal Neutral', 'Legal Neutral'),
('Legal Malvado', 'Legal Malvado'),
('Neutral Bueno', 'Neutral Bueno'),
('Neutral', 'Neutral'),
('Neutral Malvado', 'Neutral Malvado'),
('Caótico Bueno', 'Caótico Bueno'),
('Caótico Neutral', 'Caótico Neutral'),
('Caótico Malvado', 'Caótico Malvado')
)
name = models.CharField(max_length=50)
author = models.ForeignKey(Usuario, null=True, on_delete=models.CASCADE, editable=False, related_name='personajes')
alignment = models.CharField(max_length=50, choices=ALIGNMENT)
description= models.CharField(max_length=200, null=True)
likes = models.ManyToManyField(Usuario, blank=True, related_name='personaje_likes')
favorites = models.ManyToManyField(Usuario, blank=True, related_name='personaje_favoritos')
class Item(models.Model):
name = models.CharField(max_length=50)
author = models.ForeignKey(Usuario, null=True, on_delete=models.CASCADE, editable=False, related_name='items')
description= models.CharField(max_length=200, null=True)
likes = models.ManyToManyField(Usuario, blank=True, related_name='item_likes')
favorites = models.ManyToManyField(Usuario, blank=True, related_name='item_favoritos')
class Spell(models.Model):
name = models.CharField(max_length=50)
author = models.ForeignKey(Usuario, null=True, on_delete=models.CASCADE, editable=False, related_name='hechizos')
description= models.CharField(max_length=200, null=True)
likes = models.ManyToManyField(Usuario, blank=True, related_name='hechizo_likes')
favorites = models.ManyToManyField(Usuario, blank=True, related_name='hechizo_favoritos')
class Adventure(models.Model):
DURATION = (
('Corta', 'Corta'),
('Mediana', 'Mediana'),
('Larga', 'Larga')
)
name = models.CharField(max_length=50)
author = models.ForeignKey(Usuario, null=True, on_delete=models.CASCADE, editable=False, related_name='aventuras')
duration = models.CharField(max_length=50, choices=DURATION)
description = models.CharField(max_length=200)
likes = models.ManyToManyField(Usuario, blank=True, related_name='aventura_likes')
favorites = models.ManyToManyField(Usuario, blank=True, related_name='aventura_favoritos')
I think you can create a base model and then use a OneToOneField to this model in your other models.
class BaseContent(models.Model):
name = models.CharField(max_length=500)
author= models.CharField(max_length=500)
(...)
class Characters(models.Model):
base_content = models.OneToOneField(BaseContent, on_delete=models.CASCADE)
(...)
users on my project can subscribe model Board and I try to show all objects from all subscribed boards who user subscribe.
But don't know how. I search through internet but nothing help me in this.
Model board:
class Board(models.Model):
title = models.CharField(max_length=255, verbose_name='Tytuł')
slug = AutoSlugField(populate_from='title', unique=True)
image = ImageField(blank=True, manual_crop="" ,verbose_name='Tło')
body = models.TextField(verbose_name='Opis kategorii')
author = models.ForeignKey(User, on_delete=models.CASCADE)
subscribers = models.ManyToManyField(User, related_name='subscribed_boards', blank=True)
created_at = models.DateTimeField(auto_now_add=True)
Models related to(I try to show this objects from subscribed boards):
class Subject(models.Model):
title = models.CharField(max_length=255, verbose_name='Tytuł')
slug = AutoSlugField(populate_from='title', unique=True)
body = HTMLField(blank=True, verbose_name='Treść')
image = models.ImageField(upload_to='subject', null=True, blank=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name='subjects', verbose_name='Kategoria')
votes = GenericRelation(LikeDislike, related_query_name='subjectsvotes')
class Embed(models.Model):
url = models.URLField(max_length=255)
title = models.CharField(max_length=255, verbose_name='Tytuł')
description = HTMLField(verbose_name='Opis')
thumbnail_url = models.URLField(max_length=255)
html = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
board = models.ForeignKey(Board, on_delete=models.CASCADE, blank=True, null=True, related_name='embeds', verbose_name='Kategoria')
votes = GenericRelation(LikeDislike, related_query_name='embedvotes')
slug = AutoSlugField(populate_from='title', unique=True)
In view I try this:
def feed(request):
user = get_object_or_404(User, username=request.user)
feed = user.subscribed_boards.all()
embeds = feed.filter(embed__in=feed)
return render(request, 'boards/feed.html',
{'feed': feed,
'embeds': embeds})
You have a circular reference from feed to feed in you current code. The actually required code is a lot simpler:
boards = user.subscribed_boards.prefetch_related('embeds')
You can now loop over the boards and the embeds in the boards:
for board in boards:
print(board.title)
for embed in board.embeds.all():
print(embed.title)
The above is Python code to show how to access the objects; you would more likely want to do the loops in the template.
You can try the below code:
def feed(request):
user = get_object_or_404(User, username=request.user)
feed = user.subscribed_boards.all()
embeds = user.subscribed_boards.filter(embed__in=feed)
return render(request, 'boards/feed.html',
{'feed': feed,
'embeds': embeds})
Please check if it works.