I am building this simple system for a school, where students can log in to see their results at the end of every semester. I designed a model for exams with a manytomany relationship to a user. My problems is in my template am finding it hard to show a exams results related to a logged in user.
models.py
class StudentProfile(models.Model):
SEX_CHOICES = (
('Male', 'Male'),
('Female', 'Female')
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
other_name = models.CharField(max_length=30, null=True, blank=True)
birth_of_date = models.DateField(null=True, blank=True)
birth_Of_admission = models.DateField(null=True, blank=True)
nationality = models.CharField(max_length=120)
phone_number = models.CharField(max_length=15, validators=[MinLengthValidator(10)])
gender = models.CharField(max_length=120, choices=SEX_CHOICES)
home_address = models.CharField(max_length=250, null=True, blank=True, )
passport_picture = models.ImageField(upload_to='passport_picture', null=True, blank=True,
help_text='Upload the passport picture here')
def __str__(self):
return "%s %s" % (self.user.first_name, self.user.last_name)
#receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
if created:
StudentProfile.objects.create(user=instance)
instance.studentprofile.save()
class Subject(models.Model):
subject_name = models.CharField(max_length=30, null=True, blank=True)
def __str__(self):
return self.subject_name
class Year(models.Model):
year = models.CharField(max_length=30, null=True, blank=True)
def __str__(self):
return self.year
class Exam(models.Model):
TERM_CHOICES = (
('First Term', 'First Term'),
('Second Term', 'Second Term'),
('Third Term', 'Third Term')
)
level = models.ForeignKey('Level', null=True, blank=True, on_delete=models.CASCADE)
student = models.ForeignKey(Year, null=True, blank=True, on_delete=models.CASCADE)
year = models.ForeignKey(Year, null=True, blank=True, on_delete=models.CASCADE)
subject = models.ForeignKey(Subject, null=True, blank=True, on_delete=models.CASCADE)
term = models.CharField(max_length=120, default="", choices=TERM_CHOICES)
mid_term_score = models.PositiveSmallIntegerField(help_text='the marks scored for mid term exams')
End_of_term_score = models.PositiveSmallIntegerField(help_text='the marks scored for end of term exams')
class_work_score = models.PositiveSmallIntegerField(help_text='the marks scored for class work')
def __str__(self):
return self.subject + "-" + self.term
views.py
class StudentView(LoginRequiredMixin, ListView):
model = Exam
template_name = 'student.html'
context_object_name = 'student'
def get_object(self):
return self.request.user.exam
What I am trying to do is to show logged in student only his results based on the exams he has taken and but is rather showing me all. Even those he has not taken.
you can retrive those exams by set_all look_up_relationship or manually can query on Exam model.
possible way can be like this
def get_object(self):
return self.request.user.exam_set.all()
Also you can try this
def get_object(self):
return Exam.objects.filter(student_id=self.request.user.id)
Edit
If i understand you properly now, then student's current level is an important factor of this Exam filtering. We should add that too than, otherwise all exam will appear. But from your Exam model structure, i haven't see any level entity associated with User model present. I am assuming term and level actually same thing ( most likely which are not )
def get_object(self):
return Exam.objects.filter(student_id=self.request.user.id, term = self.request.user.level )
Related
I am working for a personal project that is using an API and having user authentication with JWT (but used in serializer). I wanted to implement ManyToManyField for user and city but it doesn't work properly. This is the extended model I have found and django aggregation . I want that the UserSearchLocation to store the City and when logged in to see the city, while other users will not see it until the search same city.
models.py
class UserSearchLocation(models.Model):
city_name = models.CharField(max_length=85, blank=False)
def __str__(self):
return self.city_name
class City(models.Model):
user_searched_locations = models.ManyToManyField(User,
through='UsersLocations',
through_fields=('city', 'user'),
related_name="my_cities",
blank=True)
id = models.AutoField(primary_key=True, editable=False)
location = models.CharField(max_length=85)
country = models.CharField(max_length=85, blank=True)
country_code = models.CharField(max_length=2, blank=True)
latitude = models.DecimalField(max_digits=6, decimal_places=4,
null=True, blank=True)
longitude = models.DecimalField(max_digits=6, decimal_places=4,
null=True, blank=True)
zip_code = models.PositiveIntegerField(default=0)
#users_location = models.ManyToManyField(UserSearchLocation)
def __str__(self):
return f'{self.location}, {self.country_code}'
def save(self, *args, **kwargs):
self.location = self.location.capitalize()
self.country = self.country.capitalize()
self.country_code = self.country_code.capitalize()
return super(City, self).save(*args, **kwargs)
class Meta:
verbose_name_plural = 'cities'
unique_together = ("location", "country_code")
class UsersLocations(models.Model):
id = models.AutoField(primary_key=True, editable=False)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
city = models.ForeignKey(City,
on_delete=models.CASCADE,
related_name='locations_by_users',
null=True)
To add in localhost/admin/ a City works, but when to add a UserSearchLocation I have this error:
Exception Value:
column base_usersearchlocation.user_id does not exist
LINE 1: SELECT "base_usersearchlocation"."user_id", "base_usersearch...
Your error says the city.location doesn't exist - location is a CharField on your City model - are you sure you've run migrations and don't have any naming conflicts?
I have a basic restaurant inventory tracking app that allows the user to create ingredients, menus, and items on the menus. For each item on a given menu, the user can list the required ingredients for that item along with a quantity required per ingredient for that item.
Menu items have a many-to-many relationship with ingredients, and are connected via an "IngredientQuantity" through table.
Here are my models:
class Ingredient(models.Model):
GRAM = 'Grams'
OUNCE = 'Ounces'
PIECE = 'Pieces'
UNIT_CHOICES = [
('Grams', 'Grams'),
('Ounces', 'Ounces'),
('Pieces', 'Pieces')
]
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
name = models.CharField(max_length=200)
unitType = models.CharField(max_length=200, choices=UNIT_CHOICES, verbose_name='Unit')
unitCost = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='Unit Cost')
inventoryQuantity = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='Quantity')
def __str__(self):
return self.name + ' (' + self.unitType + ')'
def totalCost(self):
result = self.inventoryQuantity * self.unitCost
return "{:.0f}".format(result)
class Menu(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
name = models.CharField(max_length=200)
timeCreated = models.DateTimeField(auto_now_add=True)
timeUpdated = models.DateTimeField(auto_now=True)
def __str__(self):
return str(self.name)
class MenuItem(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10, decimal_places=2)
ingredients = models.ManyToManyField(Ingredient, through='IngredientQuantity')
menu = models.ForeignKey(Menu, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.name
def itemCost(self):
relevantIngredients = IngredientQuantity.objects.filter(menuItem=self)
cost = 0
for ingredient in relevantIngredients:
cost += (ingredient.ingredient.unitCost * ingredient.ingredientQuantity)
return cost
class IngredientQuantity(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
menuItem = models.ForeignKey(MenuItem, on_delete=models.CASCADE)
ingredientQuantity = models.IntegerField(default=0)
def __str__(self):
return str(self.ingredient)
This is a multi-user app, so when a user creates a new item on a menu and adds ingredients to it, they should only have the option of choosing ingredients they have created, not those of other users. Here is my attempt to do that in my views:
def ItemUpdate(request, pk):
item = MenuItem.objects.get(id=pk)
user = request.user
IngredientQuantityFormset = inlineformset_factory(
MenuItem, IngredientQuantity, fields=('ingredient', 'ingredientQuantity'), can_delete=True, extra=0
)
form = ItemCreateForm(instance=item)
formset = IngredientQuantityFormset(instance=item, queryset=IngredientQuantity.objects.filter(ingredient__user=user))
if request.method == 'POST':
form = ItemCreateForm(request.POST, instance=item)
formset = IngredientQuantityFormset(request.POST, instance=item, queryset=IngredientQuantity.objects.filter(ingredient__user=user))
# rest of view...
I've searched everywhere for how to implement the queryset parameter properly, but I cannot get it to work. When creating an item on a menu, the user still has the ability to choose from every ingredient in the database (including the ones created by other users). I would like the user to only be able to choose from the ingredients they themselves created.
Does anyone know how to do this properly? Thank you!
I received some guidance on Django forums and arrived at a solution which is documented below:
https://forum.djangoproject.com/t/filter-dropdown-options-in-django-inline-formset-based-on-attribute-of-through-model/13374/3
I hope someone can help me. I am a student and a beginner in django. I am trying to allow a certain premium user to view the premium content of a specific book. I already have models for the books, users, and membership type. However, I would like to know how I can connect those 3 so that a certain user can only see just the ALLOWED PREMIUM CONTENT of ONE specific book coming from a list of books.
Here's the code sample:
class Book(models.Model):
code = models.SlugField(unique=True, help_text="Enter BIC Code", null=True)
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.CASCADE, null=True)
artist = models.ForeignKey('Artist', on_delete=models.CASCADE, null=True)
summary = models.TextField(max_length=1000, help_text="Enter a brief description of the book")
book_type = models.ForeignKey('Type', on_delete=models.SET_NULL, null=True)
isbn = models.CharField('ISBN', max_length=15, help_text='Enter ISBN number')
status = models.CharField(choices=STATUS_CHOICES, default='Ongoing', null=True, max_length=30)
genre = models.ManyToManyField(Genre, help_text="Select a genre for this book")
language = models.CharField(max_length=50, null=True,
help_text="Enter the book's natural language (e.g. English, Tagalog, Japanese etc.)")
released_date = models.CharField(max_length=50, null=True, help_text="Enter the Released Date (e.g. March 2014)")
cover = models.ImageField(upload_to='book_cover', null=True, validators=[img_file_extension],
help_text="Image File Only")
teaser = models.FileField(upload_to='book_preview', validators=[pdf_file_extension], help_text="PDF File Only")
featured = models.BooleanField(null=True, default=False)
timestamp = models.DateTimeField(default=timezone.now)
def display_genre(self):
return ', '.join([genre.name for genre in self.genre.all()[:3]])
display_genre.short_description = 'Genre'
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('book-detail', args=[str(self.id)])
def __str__(self):
return self.title
class Page(models.Model):
book = models.ForeignKey('Book', on_delete=models.CASCADE, null=True)
file = models.FileField(upload_to='book_content', validators=[pdf_file_extension], help_text="PDF File Only")
def __str__(self):
return self.book.title
def get_absolute_url(self):
return reverse('page-detail', kwargs={'id', self.id})
class Membership(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
title = models.ManyToManyField(Book)
premium = models.BooleanField(default=False)
reference = models.CharField(max_length=40, blank=True)
def __str__(self):
return self.user.username
i don't know what to do after in views.py
class PageDetailView(LoginRequiredMixin, generic.View):
def get(self, request, *args, **kwargs):
book = get_object_or_404(Book)
page = get_object_or_404(Page)
membership = get_object_or_404(Membership, user=request.user)
context={'object': None}
if #-----------------------
return render(request, "catalog/page_detail.html", context)
I am a django newbie and have one more big struggle for longer time... :/
User can choose a 'main language' which is set as ForeignKey. User can choose 'further languages' as ManyToMany (Checkbox). Assuming, user selects english as 'main' language, so english has to be filterd out from the 'further languages'... have been searching so much and have no idea how to do it. Is this even possible without JavaScript?
Of course, I could set the 'queryset' in the second form but it would filter the objects after the submit... The similar problem is, when a selected country has to be connected to the proper zipcodes...
I am very thankful for any hints.
Best regards.
class Country(models.Model):
enter code here
country = models.CharField(max_length=40)
active = models.BooleanField(default=True)
class Meta:
verbose_name_plural = 'Länder'
def __str__(self):
return self.country
class ZipCode(models.Model):
zipcode = models.CharField(max_length=5)
city = models.CharField(max_length=255)
active = models.BooleanField(default=False)
class Meta:
verbose_name_plural = 'Postleitzahlen'
def __str__(self):
return '{0} {1}'.format(self.zipcode, self.city)
class MainLanguage(models.Model):
language = models.CharField(verbose_name='Hauptsprache', max_length=40)
active = models.BooleanField(default=True)
class Meta:
verbose_name_plural = 'Hauptsprachen'
ordering = ['language']
def __str__(self):
return self.language
class SecondLanguage(models.Model):
language = models.CharField(verbose_name='weitere Sprachen', max_length=40)
active = models.BooleanField(default=False)
class Meta:
verbose_name_plural = 'weitere Sprachen'
ordering = ['language']
def __str__(self):
return self.language
class CustomUserprofile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(verbose_name='Vorname', max_length=40,
null=True, blank=True)
country = models.ForeignKey(Country, verbose_name='Land',
null=True, blank=True)
zipcode = models.ForeignKey(ZipCode, blank=True, null=True)
main_language = models.ForeignKey(
MainLanguage, verbose_name='Hauptsprache',
null=True, blank=True)
second_language = models.ManyToManyField(
SecondLanguage, verbose_name='weitere Sprachen',
null=True, blank=True)
class UserProfileForm(forms.ModelForm):
second_language = forms.ModelMultipleChoiceField(
queryset=SecondLanguage.objects.all(),
required=False,
widget=forms.CheckboxSelectMultiple)
class Meta:
model = CustomUserprofile
exclude = ('user',)
I've been following the manual for generic views for Django 1.4, but can get the 'list books by publisher' example to work. My site is slightly different in that I'm trying to list bookings of a property by the name (or id) of the person who books the property. People will book more than once, so I want to be able to see what their bookings were.
My views.url for this is:
class GuestBookingListView(DetailView):
context_object_name = 'guest_booking'
template_name = 'guest_booking.html'
def get_queryset(self):
self.guest = get_object_or_404(Guest)
return Booking.objects.filter(guest = self.guest)
def get_context_data(self, **kwargs):
context = super(GuestBookingListView, self).get_context_data(**kwargs)
context['guest'] = self.guest
return context
My model is:
class Guest(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=50)
spouse_first = models.CharField(max_length=30, blank=True)
spouse_last = models.CharField(max_length=50, blank=True)
num_child = models.IntegerField(verbose_name='Number of children')
address = models.TextField(max_length=50, blank=True)
city = models.CharField(max_length=60, blank=True, verbose_name='Town / City')
state_province = models.CharField(max_length=30, blank=True, verbose_name='County')
post_code = models.CharField(max_length=8, blank=True)
country = models.CharField(max_length=50, blank=True)
email = models.EmailField(blank=True)
landline = models.CharField(max_length=25, blank=True)
mobile = models.CharField(max_length=25, blank=True)
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Booking(models.Model):
guest = models.ForeignKey(Guest)
ack_date = models.DateField(verbose_name='Date acknowledged')
start_date = models.DateField(verbose_name='Start date')
end_date = models.DateField(verbose_name='End date')
dep_recd = models.DateField(null=True, blank=True, verbose_name='Deposit received')
bal_recd = models.DateField(null=True, blank=True, verbose_name='Balance received')
keys_sent = models.DateField(null=True, blank=True, verbose_name='Date keys sent')
sec_retn = models.DateField(null=True, blank=True, verbose_name='Security deposit returned')
rtm_sent = models.IntegerField('Status', blank=True)
notes = models.TextField(blank=True, verbose_name='Notes')
and my urls.py is:
url(r'^guests/(?P<pk>\d+)/$', GuestBookingListView.as_view (
#context_object_name = 'booking_list',
)),
So far as I can see this identical (with different field names) to the example, but the result I get is:
get() returned more than one Guest -- it returned 26! Lookup parameters were {}
The 'get' is retrieving all of the Guests in the database, not the one which I've selected.
I've spent hours of searching and experimenting on this, but to no avail. If I put 'guest = 11' it works, so there's something wrong with the pk.
Thank you!
You haven't given any sort of criteria to get the guest. You've just said, in effect, "give me guest", and Django has given you all 26 of them. If you want to filter by the pk kwarg, you should say so:
self.guest = get_object_or_404(Guest, pk=self.kwargs['pk'])