What I currently have in my models is this:
class Project(models.Model):
project_name = models.CharField(max_length=255, unique=True, blank=False)
def __str__(self):
return str(self.project_name)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
role = models.CharField(choices=ROLE_CHOICES, max_length=255, default='Agent')
Now my question is: Users should be able to have multiple Projects - so I obviously can't use a OneToOne-Field in the Profile-Model.
Later I want to use it for example to just show a user news which are only related to the projects he participates in.
What would be the best strategy to make this possible? Any input is highly appreciated.
Use ManyToMany on project.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
role = models.CharField(choices=ROLE_CHOICES, max_length=255, default='Agent')
project = models.ManyToManyField(Project)
This way one profile can have as many project as he/she wants
On your view you can use this field to filter based on project
Related
Say there is a poll app and I want a user to vote only once for a poll question.
Assume models.py to be as follows,
class PollUser(models.Model):
username = models.CharField(max_length=120, unique=True)
class PollQuestion(models.Model):
question = models.CharField(max_length=250)
issuing_user = models.ForeignKey(PollUser, on_delete=models.CASCADE)
class PollChoice(models.Model):
text = models.CharField(max_length=250)
votes = models.PositiveIntegerField(default=0)
Is there a way to implement this functionality, without making this check in the views?
I need advice on a multiple user type.
Scenario:
A user can be an organization, where in this organization they can place adverts on the website. The owner of this organization(user) can edit/delete users and adverts of his own organization(group). In this organization user type there are users that also can log in and they can only see the adverts placed by them, but the owner of this group must see all adverts of his own and of his users. Think like an estate listing where an organization has multiple locations/users that can place adverts, and has to be managed by a admin user of this organization.
What type or model is the best/cleanest for implementing this in a good way? Do I need the Django's User and Group model?
One solution would be to have the "User Profiles" structure.
So you use the standard Django User Model and you attach to it several OneToOne relationships depending on the number of profile types you'll have. This has the advantage of allowing users to cover more than one role at the same time.
For example:
from django.contrib.auth.models import User
class Organization(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="organization")
name = models.CharField(max_length=50, blank=True, null=True)
class Supervisor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="supervisor")
name = models.CharField(max_length=50, blank=True, null=True)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name="supervisors")
class CustomUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="custom_user")
name = models.CharField(max_length=50, blank=True, null=True)
supervisor = models.ForeignKey(Supervisor, on_delete=models.CASCADE, related_name="custom_users", blank=True, null=True)
And then when you go and create the models for the ads to be displayed on the website you can use the built-in PermissionRequiredMixin.
In order to do that you have to start by adding "permissions" in the ad model Meta class:
class Ad(models.Model):
# fields
class Meta:
permissions = [
('can_edit_ads', 'org_representative')
]
Then on your view you have to extend the PermissionRequiredMixin, example:
class EditAd(UpdateView, PermissionRequiredMixin):
model = Ad
template_name = "ad123.html"
permission_required = "ad.can_edit_ads"
A quick way to test it is by going in the user table on the admin panel, open a user detail page where you can see all the permissions, and there alongside the others you'll find your custom one as well.
From there you can easily assign the new permission to the specific user.
NOTE I am not necessarily asking for code to build this, just ideas on how to do this. Links and blog posts for pointers are welcome.
I am building a rest api.
I have a model
class Showcase(models.Model):
title = models.CharField(max_length=50)
description = models.TextField(null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name="Showcases")
created_on = models.DateTimeField(auto_now_add=True)
updated_on = models.DateTimeField(auto_now=True)
slug = models.SlugField(max_length=255, unique=True)
def __str__(self):
return self.title
I am trying to build a functionality where the user that created a showcase can add users that contributed to the project which is the showcase. I was thinking of making this its own model like this:
class Collaborator(models.Model):
post = models.ForeignKey(Showcase, on_delete=models.CASCADE, related_name="collaborated_showcases")
owner = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name="showcase_owner")
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name="collaborators")
skill = models.ForeignKey(Skill, on_delete=models.CASCADE, null=True, related_name="creative_type")
role = models.TextField(null=True)
added_on = models.DateTimeField(null=True)
def __str__(self):
return f"{self.user.name} collaborated on {self.post.name}"
The user would have to search for users and then add them as a contributor to the showcase, which is where my brain scrambles a bit.
The other important thing is that I want to be able to randomly go to a user and get ALL the showcases he has contributed to.
As I see it, this structure works fine for your use case, though:
models should always be in singular case (Collaborator)
related_names should be lower case (related_name="showcases")
and I prefer to explicitly spell out all related_names, so I'd add
Collaborator.post related name collaborated_showcases
Collaborator.user related name collaborators
Showcase.user related_name owned_showcases
Then,
To find an user's owned showcases, Showcase.objects.filter(user=user)
To find an user's collaborated showcases, Showcase.objects.filter(collaborators=user) (I think)
I'd suggest having a Collaborator object for the owner user as well, so you can show their role more easily as well as make these queries simpler.
I'm working on a project using Python(3.7) and Django(2.1) in which I need to build a relationship between users and organizations.
I'm using the default Django User model and a profile model to add extra information to users.
Many users can join an organization and an Organization can have many members, a user can create an Organization, these behaviors I need to implement, according to my understanding we need to build a ManyToMany relationship for Organizations model, but don know how to use this relationship to display the information, e.g display a user's organizations on his profile page.
Here are my models:
class Organization(models.Model):
name = models.CharField(max_length=255, blank=False)
users = models.ManyToManyField(User, related_name='members', null=True)
def __str__(self):
return self.name
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='media/default.jpg', upload_to='profile_pics')
goals = MultiSelectField(choices=goals_choices, default='')
def __str__(self):
return f'{self.user.username} Profile'
You can get all organizations of a particular user by:
my_user.members.all()
If you want to access from profile:
my_profile.user.members.all()
But I would suggest to remove related_name or rename it. If you remove it you can access all user organizations as:
my_user.organization_set.all()
my_profile.user.organization_set.all()
For organization you can get all users by:
my_org.users.all()
I'm using django-allauth and I want to be able to add new field to my User model.
What's the best way to do this as of you ?
I use userena. But I am sure that it will look almost the same ;)
class UserProfile(UserenaBaseProfile):
user = models.OneToOneField(User, unique=True)
city = models.CharField(max_length=32, blank=True, null=True)
in settings.py:
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
See the docs Storing additional information about users, Make a model with OneToOneField relation to User.
from django.contrib.auth.models import User
class UserProfile(models.Model):
# This field is required.
user = models.OneToOneField(User)
# Other fields here
accepted_eula = models.BooleanField()
favorite_animal = models.CharField(max_length=20, default="Dragons.")