I want to create a custom admin view. It should list specific objects from a model and add attributes to it.
More precisely, I have the tables Student and Lecture. Lecture has an attribute enrolled_students, which is a Many-to-Many relationship (Student can be enrolled in multiple lectures; a lecture can have many enrolled students). Students can gain points in a lecture, which determines their grade.
Therefore, I would like to provide an admin view, in which all students of a specific lecture are listed, along with their reached points and grade. The grade is computed lazily, as points are changing frequently.
How can I implement this? Is this possible using the built-in methods?
Thanks a lot!
EDIT 1
I added the source code (simplified):
Lecture:
class Lecture(UUIDModel, TimeFramedModel):
participants = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="enrolled_lectures",
)
title = models.CharField(
max_length=100,
unique=True,
)
grading_scale = models.ForeignKey(
GradingScale, on_delete=models.SET_NULL, blank=True, null=True
)
def get_score(self, user):
""" Returns score of a particular user. """
score = (
user.submission_set
.filter(exercise__lesson__lecture=self, score__isnull=False, exercise__rated=True)
.values("exercise")
.annotate(max_exercise_score=models.Max("score"))
.aggregate(total_score=models.Sum("max_exercise_score"))["total_score"]
)
return score if score else 0
Student (User model):
class CustomUser(AbstractUser):
username = None
email = models.EmailField(_("email address"), unique=True)
full_name = models.CharField(_("full name"), max_length=130)
)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["full_name"]
objects = UserManager()
GradingScale contains the method get_grade(score). You can find the whole source code here.
Related
well my college is making us go learn a framework and make a website with it in a month, and it's really killing me, because of that I couldn't really get a good understanding of the Django framework as I am making progress while watching YouTube vids and reading docs.
Anyways my models are all messed up which made the job even harder, and whenever I solve a problem another one arises, but the deadline is close and making any changes to the models will cost me a great deal of time. This time my problem is about fetching data.
The concerned models are the following:
The User class for authentication
class User(AbstractBaseUser, PermissionsMixin):
id = models.AutoField(primary_key=True,null=False)
username = models.CharField(max_length=50)
email = models.EmailField(unique=True)
nom = models.CharField(max_length=255)
prenom = models.CharField(max_length=255)
usertype = models.CharField(choices=types,max_length=20,default="user")
date_joined = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField(auto_now=True)
is_superuser = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
student_data = models.OneToOneField(Etudiant, on_delete=models.CASCADE,blank=True, null=True,related_name='Etudiant_access')
Prof_data = models.OneToOneField(Prof, on_delete=models.CASCADE,blank=True, null=True)
objects=UserManager()
def __str__(self):
return self.prenom + " " + self.nom
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
The Students(Etudiant) class for managing the students:
class Etudiant(models.Model):
filiere = models.ForeignKey(Filiere, on_delete=models.DO_NOTHING)
classe = models.ForeignKey(Classe,null=True, on_delete=models.DO_NOTHING)
notes = models.ManyToManyField(note,blank=True, null=True)
The class Classe (LMAO) for managing the different classes:
class Classe(models.Model):
#Cla_id = models.AutoField(primary_key=True, null=False)
Designation = models.CharField(max_length=100)
filiere = models.ForeignKey(Filiere, on_delete=models.CASCADE)
Epreuve = models.ManyToManyField(Epreuve,blank=True, null=True)
def __str__(self):
return self.Designation
The thing is that I wanna fetch all data of the Users that are students (which means that their Prof_data attribute is blank/null and their student_data attribute is pointing to the Etudiant(Student) class while having an Etudiant.classe attribute equals to a value in the view's parameters
I've solved a great deal of it but I'm stuck at the end
This is my view function:
#login_required
def class_info(request,design):
#getting the Classe(s) from the url which Designation == design
classe_now = Classe.objects.get(Designation=design)
print(classe_now) # This works like a charm
#getting the Students objects that are part of the class_now
Etudiants = Etudiant.objects.filter(classe=classe_now)
print(Etudiants) # This works too. It returns the 'Etudiant' objects from where i wanna access to the Users data
#getting the User data of the student objects (This is where i get confused)
students_to_show = User.objects.filter(student_data=Etudiants)
pprint(students_to_show)
return render(request, 'Prof/class_info.html')
I am really confused, you are truly my last hope in this, and thank you for your time.
You can filter your User model by selecting all users that do have empty/null relation to Prof model and nonempty/null relation to Etudiant model.
student_users = User.objects.filter(Prof_data__isnull=True, student_data__isnull=False)
then for each stident_user, you can fetch its student data in the following manner:
student_user = student_users[0]
student_user.student_data.filiere
student_user.student_data.classe
student_user.student_data.notes
You can then pass the queryset result to the render function as a context variable. Check this brief tutorial on how to pass data to templates.
i have a course models and user are allowed to purchase this courses, now i want to filter the courses by the most selling course, i have tried using django agreegate but it seems not to be giving me what i want or maybe i am not doing it the right way.
i have models.py that is storing the courses
class Course(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
course_title = models.CharField(max_length=100, null=True, blank=True)
slug = models.SlugField(unique=True)
course_creayor = models.ForeignKey(User, on_delete=models.CASCADE)
i also have this model that stores the courses that are purchased and enrolled in
class UserCourse(models.Model):
user = models.ForeignKey(User , null = False , on_delete=models.CASCADE)
course = models.ForeignKey(Course , null = False , on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True)
i dont know how to write the view to filter the courses by the most selling
from the image below you can see that learn angular updated is the most selling course for the creator destiny. so how do i filter this for each user, thier most selling course?
You can try annotating the course counts like this:
course_counts = UserCourse.objects.values("course").annotate(count=models.Count("course"))
Then sorting:
sorted_courses = sorted(course_counts, key=lambda x: x['count'],reverse=True)
You will have the sorted_courses[0] containing the highest one and you can retrieve the UUID from the dict
I am trying to populate a dropdown in the Django admin panel based on a selected item.
I have a customer model
class Customer(BaseModel):
name = models.CharField(max_length=128)
company = models.ForeignKey("Company", models.PROTECT)
def __str__(self) -> str:
return f"{self.name}"
def save(self, **kwargs):
return super().save(**kwargs)
An invite model
class Invite(BaseModel):
full_name = models.CharField(max_length=128, blank=True)
email = WIEmailField(unique=True)
customer = models.ForeignKey(
to="Customer",
on_delete=models.PROTECT,
related_name="invites",
)
Customer Invite model that defines the invite and customer
class CustomerLocationInvite(BaseModel):
location = models.ForeignKey(
to=Location
)
invite = models.ForeignKey(
to=Invite,
blank=True,
)
Location Model
class Location(BaseModel):
name = models.CharField(max_length=128)
company = models.ForeignKey(
to= Company,
on_delete=models.PROTECT
)
address = models.CharField(max_length=128)
Inline for invite
class CustomerInviteInline(admin.TabularInline):
model = CustomerLocationInvite
fields = ("invite", "location", "created_at", "modified_at")
readonly_fields = ("created_at", "modified_at")
extra = 0
When creating a new Invite, Is it possible to:
Display the inline once a company has been selected?
When selecting a location from the inline, Filter out the locations based on the company they selected?
I assume you want to get the location from CustomerLocationInvite, and get the company from the location? If that is so, maybe you can try
CustomerLocationInvite.objects.filter(location__id=locationid)
Where the location's id can be received by
locationid = Location.objects.filter(company__id=companyid).id
But then since you said that the company is already selected, you should have its ID -- that is the value you will replace companyid with. You can then retrieve the inline admin from the CustomerLocationInvite object that you received in the first line.
SO reference
Doc reference
I'm not sure what you mean. Are you getting the list of companies, or are you getting the list of locations? That being said, please paste your Company model as well.
I am currently trying to create a health network website in Django.
The idea is that there will be a class called User inside my registration application. One of the states stored inside User is which hospital the user is registered into.
I created another Hospital within the registration app. I want to user that model Hospital as one of the model field for the hospital_used state. How do I do that? Below is a portion of my UML that illustrates the relationship
UML Diagram
Below is a portion of my UML that illustrates the relationship
png
Here is the code I have for it so far. The code where it is encapsulated with an asterisk is what I need help with.
class Hospital(models.Model):
hospital_Name = models.CharField(max_length=150)
def __str__(self):
return "Hospital Name: " + str(self.hospital_Name)
class User(models.Model):
PATIENT = 'Pat'
DOCTOR = 'Doc'
NURSE = 'Nurse'
ADMINISTRATOR = 'Admin'
user_type_choice = {
(PATIENT, 'Patient'),
(DOCTOR, 'Doctor'),
(NURSE, 'Nurse'),
(ADMINISTRATOR, 'Administrator'),
}
name = models.CharField(max_length=50)
dob = models.DateField(auto_now=False)
username = models.CharField(max_length=50)
*preferred_hospital = Hospital(models.CharField(max_length=50))*
patient_type = models.CharField(
max_length=5,
choices=user_type_choice,
)
Thank you StackOverflow Buddies
I would advise you to read this material on tutorials on how to create simple models.
What you want here is to use the ForeignKey method.
name = models.CharField(max_length=50)
dob = models.DateField(auto_now=False)
username = models.CharField(max_length=50)
preferred_hospital = models.ForeignKey(Hospital, on_delete = models.CASCADE)
patient_type = models.CharField(
max_length=5,
choices=user_type_choice,
)
You do not have to use on_delete = models.CASCADE but it is best that you handle what should happen when you delete an Hospital.
Know that you can also have OneToOne, ManyToOne, or ManyToMany fields, that are all described here.
My Users has phone contact LIST(s) - [3121234567,2121234567,6601234567]
Now, I want each user to be able to store as many LIST as possible. Each List must have a name(or description) attached to them under each USER account. Note: the number of LIST is dependent on Users needs. Example:
Students
[3121234567,2121234567,6601234567]
Club Member
[8101234567,8151234567,8171234567]
Now, how do I store it in a database.
Django User Model
class CustomUser(AbstractBaseUser):
username = models.CharField(max_length=254, unique=True)
first_name = models.CharField(max_length=24)
last_name = models.CharField(max_length=30)
phone = models.CharField(max_length=10)
email = models.EmailField(max_length=128)
street = models.CharField(max_length=128)
city = models.CharField(max_length=128)
state = models.CharField(max_length=2, choices=STATE_CHOICES, default=STATE)
zip_code = models.IntegerField(max_length=5, null=True, blank=True
USERNAME_FIELD = 'email'
REQUIRED_FIELD = ['username', 'first_name', 'last_name', 'phone', 'street', 'city', 'state']
objects = CustomUserManager()
Edit(Added):
I am not looking to create Student or ClubMember models. This name is use to identify the python phone contact list. PhoneAddress one can be labelled(named) as Student for one user but called "Conference Attendant" for another. Each user have different # of Lists.
PhoneAdress PhoneAdress2 PhoneAdress3
[3121234567,2121234567,6601234567] [4121234567,3121234567,6601234567] [7121234567,8121234567,9601234567]
Lemme guess, you're coming from a NoSQL background where the database is a document in a JSON form?
If so, I am sorry, in a Relational Database, used by Django in the likes of PostgreSQL or MySQL, they call something Foreign Keys, and that is your way of storing multiple "Lists" related to a particular field.
If you want many users to store as many lists as possible, you're looking at something like this, roughly speaking:
class myUserModel(models.Model):
# your arbitrary fields here
# then you do something like this:
class Student(models.Model):
user = models.ForeignKey(User)
class clubMember(models.Model):
user = models.ForeignKey(User)
With the above setup, you can add as many Student objects associated to the myUserModel class, so as the clubMember
However, if you wish to use PostgreSQL specifically, as your backend (perhaps as perpetual storage backend), you might find some sense in using Django's support for the ArrayField
And ooh, you might need to extend the Django User Model to add any extra fields easily, unless you're willing to go down the road of a custom User Model.
More info:
Django ForeignKey
This SO answer on 'OneToManyFields', similar to adding multiple items to a single field.
I hope the above helps
Create some models:
class Club(models.Model):
name = models.CharField(max_length=256,blank=True,null=True)
date_open = models.DateField()
class Institution(models.Model):
name = models.CharField(max_length=256,blank=True,null=True)
address = models.CharField(max_length=256,blank=True,null=True)
type = models.CharField(max_length=256,blank=True,null=True) #university, college
Rather than using
class CustomUser(AbstractBaseUser):
username = models.CharField(max_length=254, unique=True)
first_name = models.CharField(max_length=24)
last_name = models.CharField(max_length=30)
use composition in the form of OneOnOneField
class UserProfile(models.Model):
user = models.OneOnOneField(User,blank=True,null=True)
club_member = models.ManyToManyField(Club,blank=True, null=True)
institutions = models.ManyToManyField(Institution,blank=True, null=True) # student in
Once you have this, you will be able to get and add as many institutions and clubs to the lists:
user = User.objects.get(id=user_id)
club = Club.objects.get(id=club_id)
institution = Institution.objects.get(id=institution_id)
user.profile.clubs.add(club)
user.profile.institutions.add(institution)
So to verify if the user is a member of a club
club = user.proile.clubs.get(id=club_id)
and to verify the user is a student in an institution use
institution = user.profile.institutions.get(id=institution_id)