Three way modeling django - django

I want to model a schema for an award function in django. I have a User model, a show model and an award model. A user can get one award for a show. I can simply say User 'aaa' gets award 'bbb'. but then that award cannot be associated with other user for another show.
I think there is three way modeling required for User, show and Award. Is there any sophisticated way of doing it? I know about 'through' but dont think this can be a good tool here. If it is could you please guide me how to?

You can have an extra model, I will call it UserAward.
class UserAward(models.Model):
user = models.ForeignKey(User)
award = models.ForeignKey(Award)
show = models.ForeignKey(Show)

When I try to enforce two unique constraints only later one stays.

Related

exclude many-to-many with specific value in admin site

I have a model:
class Rent(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
bikes = models.ManyToManyField(Bike)
when I create a new rent in the admin site, I would like to exclude the bikes that have the value of 1 for their status integer field from showing in the list to choose from. Is this possible?
Thanks
I am not sure if you can do that from the Admin site. probably you cannot. But you can do it from views. just select the model objects you need with query and pass it into the field. I am attaching a ell explained similar scenario. condition your query and add those objects.
Adding many to many fields based on condition
You can customize the admin form. Here is a discussion of how to filter your form choices.
Thank you all! I figured it out, the problem was that the limit_choices_to doesn't work when I change the filter to inline and I found others with the same problem, so I just changed it to filter_horizontal and now it works as it should

Should Entry.author be a ForeignKey to User or UserProfile?

In Django, I want to filter a QuerySet using a list of Users who the active user is following.
I've opted to extend the User class rather than replace it with a custom class, although I'm not sure that was the right choice.
Hence what I have is a UserProfile class, which has a ManyToManyField to other UserProfiles, and a OneToOneField with User.
My QuerySet looks like Entry.objects.filter(author__in=request.user.userprofile.following.all()) but author is a ForeignKeyField to User rather than UserProfile, so I'm about to change Entry.author to point to UserProfiles instead.
So my questions are, in decreasing priority:
Is it right to have author be a UserProfile instead? Because then I have something like entry.author.user.username which is not intuitive.
Might it be better to just replace the builtin User class with a custom class which has the data I need?
Is it right for UserProfile's following to be a ManyToManyField to other UserProfile rather than to User?
I don't recommend this at all. As you said it's not intuitive, an
author should be a user.
No, a one to one relationship to user is better than creating a custom user class.
I think it will look better if you connect followings to users. This way your original query will also work.
In UserProfile model:
following = models.ManyToManyField(User, related_name="followed_by")
In this scenario user.followed_by is a list of UserProfiles, but user.userprofile.following is a list of users.
Since your users can follow each other and Entries are from those people, it makes totally sense to make the author UserProfile so that both models are logically in the same level

django - many-to-many and one-to-many relationships

I am working in django, am planning a database for rides for users.
each User can be on multiple Rides (over time) and each Ride can have multiple Users (passengers) in it.
Also, for each Ride there has to be only one Driver (also a User) so I think I have a many-to many relationship between the Rides and Users tables for what user is on what ride, and also a One-To-Many relationship between the Rides's Driver_id and the User_id. right?
My questions are-
I saw in the django docs that I should put a many-to-many field in One of the models. Does it matter which one? and also, does it create a new table like rides_users?
and also, what is the difference (in One-To-many relationship) between using a foreignKey field and a OneToManyField field?
EDIT:
Currently, there are my models:
def get_image_path(models.Model):
return os.path.join('photos',str(instance.id),filename)
class UserProfile(models.Model):
user=models.OneToOneField(User)
phone_number=models.CharField(max_length=12)
profile_picture=models.ImageField(upload_to=get_image_path, black=True, null=True)
class Ride(models.Model):
driver=models.ForeignKey(UserProfile, related_name="r_driver")
destination=models.ForeignKey(Destination, related_name="r_final_destination")
leaving_time=models.DateTimeField()
num_of_spots=models.IntergerField()
passengers=models.ManyToMany(UserProfile, related_name="r_passengers")
mid_destinations=models.ManyToMany(Destination, related_name="r_mid_destinations")
class Destination(models.Model):
name=models.CharField(max_length=30)
As you can see, each Ride has multiple mid_destination and multiple passengers. a Ride also has One driver and One final destination.
The Issue is - when a User adds a Ride, I want the driver, destination and mid_destinations and the rest of the fields to be set by the User (the driver is user adding the Ride), Except for the passengers field. I want the other Users to add themselves to the ride, so when the Ride is created the User (driver) doesn't have to set the passengers.
How do I go about it? and also, any other suggestions about the models?
There is no such thing as a OneToManyField.
It doesn't matter from a practical point of view which side the ManyToManyField lives on. Personally, I'd put it on Ride, both to avoid changing the User model and because conceptually I'd say that rides are the main objects here.
And yes, adding the field will automatically create the linking table.
what you want is probably something like this
class MyModel(models.Model):
driver = models.ForeignKey(to=User, related_name='r_driver')
# you need to use a related name if you want to link to the same model more than once
passengers = models.ManyToManyField(User, related_name="r_passengers")

dependent fields in django admin

I want to add, some fields depending on others. I have city and country model. I can include country as foreign key in city model. And then if I will add both city and country in another model ( say content) then will it be just like dependent selectboxes? like cities will be shown based on selected country via ajax? If not then what is correct way? and also is there a way to add city on the spot while adding main content data if city is not already on list?
So are above possible by using django admin or is it that django don't give? If not then how can it be done in django autogenerated admin?
You can do exactly what you ask using django-smart-selects
Hope that helps...
I can include country as foreign key in city model
This seems like a good idea.
And then if I will add both city and country in another model ( say content) then will it be just like dependent selectboxes? like cities will be shown based on selected country via ajax?
No, it will not get filtered automatically, you will need to write that code yourself. Both in admin and on frontend.
and also is there a way to add city on the spot while adding main content data if city is not already on list?
You will get this in the admin area.
Go ahead and start doing it, and when you run into specific problems, post them here if you can't solve it. Also read the Django docs, it is pretty elaborate on the topic of models.

two foreign keys back to User and calling get_full_name in Admin

Two questions please:
I need two foreign keys back to User, one for author and one for coauthor. I have managed by setting related_name equal to + but am not sure if this is smart or the best way. Thoughts or pointers?
When making an add entry via the django admin for this model, the author choices are names like john_smith, etc. Where would I call get_full_names() from in order to display full names rather than usernames with underscores? Thanks.
from django.contrib.auth.models import User
from django.db import models
class Books(models.Model):
title = models.CharField()
author = models.ForeignKey(User)
coauthor = models.ForeignKey(User, related_name='+')
Kevin
I would change the related name to a value that is more intelligible - such as books_where_coauthor and also add a similar one of books_where_author as then you can get the relevant books by going from theuser.books_where_author.all() etc
Regarding your Admin query, you're getting the username because that's what the default __unicode__() method of User spits out.
Unless you'd like to hack your contrib.auth.models file (not recommended), I'd suggest using a custom modelform in the admin, and manually setting the names of the choices in the ModelChoiceField, either by subclassing that field and making a custom one that renders its widget with get_full_name if possible, or do it via something like this snippet. That said, I am sure there's a simpler way to do that, but I've forgotten. Dangit.
With regard to the second part of my question (displaying full names instead of usernames in the ChoiceField of a form), I found this link to be just the ticket: