I have the following two models (only included their relationship to each other). This is a job board site, a business owner can create one or more Business objects (on the off chance they own more than one small business) and then post as many Job objects as they please.
class Business(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
class Job(models.Model):
business = models.ForeignKey(Business, on_delete= models.CASCADE)
How can I get all Job objects belonging to a User? I know I can get all Job objects belonging to a Business but a user can create multiple businesses.
I know I have to build some kind of chain filter, I am just not sure how to go about that.
Edit: I am trying to achieve this so I can display all of a user's posts in a dashboard type of view.
You can do:
Job.objects.filter(business__user=user)
Note the double underscore after "business". That's how you access the business' attributes
Related
I have one model called Weight (filled by User input/choice) and another called Enterprise.
class Weight(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="weights")
weight_of_history = models.IntegerField(null=True, blank=True)
class Enterprise(models.Model):
...
The weight is saved, one per user, and replaced everytime the user choose new one.
Inside the Enterprise class, I am creating an property that must get the "weight_of_history" (depending on the user, who has chosen the weight) from Weight class, but the models have no Foreign key or related name between them.
class Enterprise(models.Model):
...
#property
def ranking(self):
weight_of_history = <-- HERE I NEED TO TAKE WEIGHT_HISTORY FROM THE FIRST MODEL
THEN I COULD CALCULATE
How could I do that? Thank you!
You can use django's powerful query functionality and fetch the required objects from the db. Here are the docs that might help you with that. Django docs are amazing, so I would recommend that you read up on queries, models, and forms to have easier time with you project.
For your example we can fetch all the weights for the user in one query by filtering the weights by user. In fact, django ORM allows for chaining filters and you can create really sophisticated queries
class Enterprise(models.Model):
...
#property
def ranking(self):
weight_of_history = Weight.objects.filter(user=some_user)
If you do not know the user beforehand, then you can do inside the view and grab the user that makes the request and filter using this user:
#views.py
user_making_request = request.user
weight_of_history = Weight.objects.filter(user=user_making_request)
I'm creating a Django project to help people run clubs and community groups. I'm new to Django and I can think of lots of different ways to add users to clubs and manage permissions, but it's not clear what the right way to do it is.
I have a "Club" model and a "Team" model. A club can have many teams, and members of a club may or may not be members of a team, but a user must be a member of the club in order to join the team. There will also be various permissions around what a club/team member can and cannot do (and what a club/team administrator can do)
My current plan for managing membership is to create "ClubMembership" and "TeamMembership" models and create the relationships using ManyToManyField.through, as demonstrated here: https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.ManyToManyField.through
For permissions, it looks like I should use django-guardian and set permissions when people join/leave a club/team. Would it make sense to also have a "members" foreign key to django.contrib.auth.models.Group in my club/team models to keep track of this?
It feels like I'm doubling up in some areas by having membership managed by membership models and then also using groups to set permissions. Is there a better way to approach this, or anything I should modify/consider?
class Team(models.Model):
# Fields
name = models.CharField(max_length=255)
# Relationship Fields
club = models.ForeignKey(
'organizations.Club',
on_delete=models.CASCADE, related_name="teams",
)
members = models.ManyToManyField(
'organizations.Users.user',
related_name="teams", through='TeamMembership', through_fields=('team', 'user')
)
administrators = models.OneToOneField(
Group,
on_delete=models.CASCADE, related_name="teams",
)
class TeamMembership(models.Model):
# Fields
name = models.CharField(max_length=255)
# Relationship Fields
team = models.ForeignKey(
'organizations.Team',
on_delete=models.CASCADE, related_name="teammemberships",
)
user = models.ForeignKey(
'users.User',
on_delete=models.CASCADE, related_name="teammemberships",
)
To summarize my questions:
Is the "TeamMembership" model the right way to associate users with
Teams?
Should I include Groups in my models to house users for
permissions purposes, or will this cause problems?
Is there another approach I should consider?
Welcome to StackOverflow.
1) Team Membership looks like a good way to do it. I'm not sure what the name field would be used for but it looks like it may be redundant (from Team name or user name).
2) It would not be a bad idea to implement Groups for permissions. That way the security part of your app is not dependent on the functional part, and vice versa.
3) The best way to implement django-guardians permissions with Groups would be to use signals that are sent whenever a Team or TeamMembership instance is created or destroyed. The creation/deletion of a Team would result in the creation/deletion of a corresponding permissions group, and the creation/deletion of a TeamMembership would result in the addition/removal of that person in the Team permissions group.
Assume I have a very simple model:
class School(models.Model):
name = models.CharField(max_length = 100, unique=True)
I want to allow unauthenticated users to use a modelform to suggest changes to School objects, but I want to flag those changes as not yet being seen by an administrator. Once an administrator approves, I will then make the suggested change to the existing School object.
What is the best way to do this? Do I need to subclass the School class, perhaps calling it UpdateToSchool and allowing users make suggestions on this subclassed model rather than the target model itself?
Here is one way you can address this, to have a SuggestedSchoolEdits (or something like that) class that would hold attributes such as:
class SuggestedSchoolEdits(object):
school = models.ForeignKey(School) #You could use generic foreign key to extend this to any type - not just school
field = models.CharField(choices=<list of fields user can edit>)
value = models.TextField()
user = models.ForeignKey(User, null=True, blank=True) #if you want approval for logged in users too
moderator_approved = models.BooleanField()
approver = models.ForeignKey(User)
#Whatever else you wish to track
Now, when an edit is made, in the view, you can create an object of this type instead of updating the existing object. Once a moderator approves, a post_save signal could trigger the update of the School object.
This way, you have complete control over which one gets approved, rejected, etc. and you can keep track of suggestions, etc..
In my Django app, I want to allow users to see which profiles they view and which profiles view them. In my Profile model I have created 2 fields that accomplish this.
viewed = models.ManyToManyField('self', null=True, blank=True, related_name='viewed_profiles', symmetrical=False)
visitors = models.ManyToManyField('self', null=True, blank=True, related_name='visitors_profiles', symmetrical=False)
I also have the code set up in my views.py file to add profiles to these fields as necessary. However, I would like to only track and display the most recent 25 or so viewed and visitor profiles. Is there a way to query these fields ordered by date added and delete everything past the first 25 results? Is this possible without creating another field to track the order of the profiles viewed?
Take a look at the documentation on Querysets for details of how to do this. You can use order_by to order your objects by date, and use Python's array slicing syntax to limit the number of results.
An example of showing the most recently added items in your view might look something like this:
viewed = Profile.objects.order_by("-date_added")[:25]
This doesn't delete everything after 25 - it just fetches the 25 most recent objects (assuming your Profile model has a field called date_added).
EDIT: Oops, I think I misread your question.
I think what you'd need to do is have an intermediate model - Django allows you to use a third model as an intermediate one between two different models in a many-to-many relationship. Then you could add the time viewed to that model and store it that way. There's a good example in the documentation.
I wouldn't really bother deleting the old ones unless database space was likely to be an issue, but if you need to for any reason, I guess you could set up a signal that was triggered by a new view being created and have that call a function that deletes all but the 25 most recent.
Django doesn't track the date added for a ManyToMany relationship, so it's not possible to do this reliably without adding a field. To achieve this you'll need to do is add a date field on your ManyToMany intermediary table, then order by that - for example
class ProfileViewed(models.Model):
viewed = models.ForeignKey('Profile')
viewer = models.ForeignKey('Profile')
date_added = models.DateField(auto_now_add=True)
class Profile(models.Model):
...
viewed = models.ManyToManyField('self', null=True, blank=True, related_name='viewed_profiles', symmetrical=False, through=ProfileViewed)
Then you can order your results like so:
profile = Profile.objects.get(...)
views = ProfileViewed.objects.filter(viewed=profile).order_by('date_added')
django guardian https://github.com/lukaszb/django-guardian is a really well written object-level permissions app; and I have actually read up on and used quite a number of other django object level permissions app in various django projects.
In a recent project that I am working on, I decided to use django guardian but I have a model design question relating to the pros and cons of two possible approaches and their respective implications on sql query performance:-
using django.contrib.auth.models.Group and extending that to my custom organization app's models; or
using django.contrib.auth.models.User instead and creating an m2m field for each of the organization type in my organization app.
Approach #1
# Organisation app's models.py
from django.contrib.auth.models import Group
class StudentClass(models.Model):
name = models.CharField('Class Name', max_length=255)
groups = models.ManyToManyField(Group, blank=True)
size = models.IntegerField('Class Size', blank=True)
class SpecialInterestGroup(models.Model):
name = models.CharField('Interest Group Name', max_length=255)
groups = models.ManyToManyField(Group, blank=True)
description = models.TextField('What our group does!', blank=True)
class TeachingTeam(models.Model):
name = models.CharField('Teacher Team Name', max_length=255)
groups = models.ManyToManyField(Group, blank=True)
specialization = models.TextField('Specialty subject matter', blank=True)
In this approach, when a user is added to a group (django group) for the first time, the group object is created and also assigned to one of these 3 classes, if that group object does not yet belong to the class it is added into.
This means that each StudentClass object, sc_A, sc_B etc, can possibly contain a lot of groups.
What that means is that for me to ascertain whether or not a specific user (say myuser) belongs to a particular organization, I have to query for all the groups that the user belong to, via groups_myuser_belongto = myuser.groups and then query for all the groups that are associated to the organization I am interested in, via groups_studentclass = sc_A.groups.all() and since I now have 2 lists that I need to compare, I can do set(groups_myuser_belongto) && set(groups_studentclass), which will return a new set which may contain 1 or more groups that intersect. If there are 1 or more groups, myuser is indeed a member of sc_A.
This model design therefore implies that I have to go through a lot of trouble (and extra queries) just to find out if a user belongs to an organization.
And the reason why I am using m2m to groups is so as to make use of the Group level permissions functionality that django guardian provides for.
Is such a model design practical?
Or am I better off going with a different model design like that...
Approach #2
# Organisation app's models.py
from django.contrib.auth.models import User
class StudentClass(models.Model):
name = models.CharField('Class Name', max_length=255)
users = models.ManyToManyField(User, blank=True)
size = models.IntegerField('Class Size', blank=True)
class SpecialInterestGroup(models.Model):
name = models.CharField('Interest Group Name', max_length=255)
users = models.ManyToManyField(User, blank=True)
description = models.TextField('What our group does!', blank=True)
class TeachingTeam(models.Model):
name = models.CharField('Teacher Team Name', max_length=255)
users = models.ManyToManyField(User, blank=True)
specialization = models.TextField('Specialty subject matter', blank=True)
Obviously, this model design makes it really easy for me to check if a user object belongs to a particular organization or not. All I need to do to find out if user john is part of a TeachingTeam maths_teachers or not is to check:
user = User.objects.get(username='john')
maths_teachers = TeachingTeam.objects.get(name='Maths teachers')
if user in maths_teachers.users.all():
print "Yes, this user is in the Maths teachers organization!"
But what this model design implies is that when I add a user object to a group (recall that I want to use django guardian's Group permissions functionality), I have to make sure that the save() call adds the user object into a "Maths Teachers" group in django.contrib.auth.models.Group AND into my custom TeachingTeam class's "Maths Teachers" object. And that doesn't feel very DRY, not to mention that I have to somehow ensure that the save calls into both the models are done in a single transaction.
Is there a better way to design my models given this use case/requirement - use django groups and yet provide a way to "extend" the django's native group functionality (almost like how we extend django's user model with a "user profile app")?
My take on this (having developed django apps for a long time) is that you should stick with the natural approach (so a StudentClass has Users rather than Groups). Here "natural" means that it correspond to the actual semantics of the involved objects.
If belonging to a specific StudentClass must imply some automatic group (in addition to those granted to the user), add a groups m2m to the StudentClass model, and create a new authentication backend (extending the default one), which provides a custom get_all_permissions(self, user_obj, obj=None) method. It will be hooked by https://github.com/django/django/blob/master/django/contrib/auth/models.py#L201
In this method query for any group associated to any Organization the user belongs to. And you don't need to do 1+N queries, correct use of the ORM will navigate through two *-to-many at once.
The current ModelBackend method in https://github.com/django/django/blob/master/django/contrib/auth/backends.py#L37 queries get_group_permissions(user_obj) and adds them to the perms the user has assigned. You could add similar behavior by adding (cached) get_student_class_permission and other corresponding methods.
(edited for clearer prologue)
Obs: There is another approach which is to use generic relationships, in this approach you would have the User model instance pointing to it's resources through the contenttypes framework. There is a nice question here on SE explaining this approach.
About the performance: There are some clues that a single select with JOIN is cheaper than many simple select (1,2,3). In this case opition 2 would be better.
About the usability: The first approach is hard to explain, hard to understand. IMHO go for no. 2. Or try the generic relations.