I have a profile.
class UserProfile(models.Model):
user = models.ForeignKey(User, related_name="%(class)s", unique=True)
providers = models.ManyToManyField(ServiceProvider, related_name="%(class)s")
class ServiceProvider(models.Model):
name = models.CharField(max_length=200, null=False, default="ims")
How do I get to the user object with just having the profile object.
Can I do: (assuming I have my service provider object)
provider.userprofile.user.get() // or something like that.
I'd like to do that in one sql query. So if I had just the pk of the provider, it would be great to get to the user profile and/or the user that holds that provider.
Since your User is just a foreign key, all you need is provider.userprofile.user
If you don't want that to incur a second SQL query, you can just use the select_related option when selecting your profile, like:
UserProfile.objects.get(pk=<the_id>,select_related=True)
As written, your models don't provide a trivial way to get from the provider to the User, since there is a ManyToMany from the ServiceProvider to the UserProfile. You'll have to get the set of UserProfiles associated with the provider, get the one you want, and then proceed to get the User as above.
Related
I have a company table as follows:
class Company(models.Model):
users = models.ManyToManyField(settings.AUTH_USER_MODEL, default=None)
My plan is that a given company instance can have more than one user account tied to it.
But from above, when a company is created, the users field is ending up with all the users in the users table.
That is not I want. I want that users are manually added rather than just populating the field with all users in the database:
How can I set this up so that a given company instance can only have the users added to it rather than all database users?
class Building(models.Model):
address = models.CharField(max_length=20, primary_key=True)
employers = models.ManyToManyField(
settings.AUTH_USER_MODEL, related_name="employers",
blank=True)
Suppose n users of model type User and m buildings of model type Building (m << n). I would like in the Admin page to be able to put users into building in the unique way:
a user can be in maximum one building.
a building can have many employers. It can be empty too.
in the Admin page, in the Employers selection widget, in the UPDATE
mode, exclude users that belong to another building. In the CREATE
mode, show only users without a building.
Stack: Django, MySQL.
So, basically you need inside User model one field with Foreign key relationship with Building and you can query it with related name.
example:
class User(AbstractUser):
"""
Your user model
"""
building = models.ForeignKey(Building, related_name='building_employers', on_delete...)
...
Later you can query employers with building_object.building_employers.all()
For question number 3, please check:
https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.TabularInline
I have a web application for businesses. Upon login, a user (the manager of the business) can view tables with information about their business.
userID 1->* rows in a MasterTable,
userID 1->* rows in a ForecastTable
I need to change it so an owner of multiple businesses can log into the account for a specific business and edit the same information that the manager can.
I was thinking about changing the database schema to something like this:
userID - businessID 1-* rows in MasterTable, rows in ForecastTable
Should I have 2 login pages, first for the userID, then the businessID. Then all the columns in the database only reference the businessID, so different users can edit the same data.
Or the same login form makes the user enter a businessID, and their username, then depending on which businessID they enter, it logs into that page?
I'm not sure what is the best practice to implement something like this.
Here is what my django model looks like:
from django.db import models
from django.contrib.auth.models import User
class MasterEntry(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField()
cutoff1 = models.IntegerField(default=0)
cutoff2 = models.IntegerField(default=0)
rooms_sold = models.IntegerField(default=0)
is_blackout = models.BooleanField(default=False)
class ForecastEntry(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField()
rate = models.IntegerField(default=0)
A user has hundreds of these 'master entry' and 'forecast entry' rows. I query the database for the rows and create a table in the front end.
You don't need two layers of authentication.
For instance, a user which is not admin only can view business he owns, this is achieved with a simple filter in the view displayed the mentioned list of business.
And you could render each business name as a link that then shows a list of MasterEntrys in it.
This is more a problem with information layout.
Conclusion:
Show a page only with business belonging to the authenticated user.
Superuser can see all business.
You can click a business entry in order to view/edit/delete any of the MasterEntrys it contains.
I have a project that publishes job offers, a job seeker can create a profile on the website and his profile page will have URL like:
www.domain_name.com/slug
An institution publishing job offers can also create an account.The person actually creates the institution account will be considered as the administrator of the institution page, so he will have an account (*instance of UserProfile() and can add other members (Users on the platform not affected to any institutions) to that institution. Institution profile URL will be www.domain_name.com/institution_slug
Members will have their own UserProfile instance linked to that institution, and once connected, only the institution page data/functionalities will be visible, they will work as the institution.
So What is the best way to have these 2 models and handle the URL pattern properly? like
urls.py
path("<slug:slug>",profile,name='profile')
views.py
def profile(request,slug):
user = UserProfile.objects.get(slug=slug) #get_object_or_404
inst = Institution.objects.get(slug=slug) #get_object_or_404
In above example, I don’t want to have to query twice in two different models. Is there another way to do it? Or If I need to re-design the database structure, let me know!
models.py
UserProfile(models):
# info_about_user
sug = models.SlugField()
institution = models.ForeignKey(Institution)
Institution(models.Model):
# info_about_instituion
slug = models.SlugField()
Note that both may publish job offers:
Job(models.Model)
owner = UserProfile or Institution
I try to build a model, e.g. Userprofile, in which fields will be chosen in the admin panel.
class UserProfile(models.Model):
user = models.OneToOneField(User)
regfield = models.ForeignKey(Regfields)
This model refers to a second model Regfields:
class Regfields(models.Model):
field_name = models.CharField(max_length = 256, unique=True, blank=False)
field_type = models.ForeignKey(FOREIGNKEY_TO_DATATYPES)
I want to choose the Fieldname and the Fieldtype (e.g. Char_field ...) to use this in a second model Userprofile, to refer to the field names and field types in the Regfields model. What is the best approach to realize this?
Edit:
I don't seek to have a kind of dynamic database for the user profile. I think I could achive something like this by using a "static" choice list, e.g.:
FIELD_TYPE_CHOICES = (
(BigIntegerField,'Long Int'),
(BooleanField,'Boolean'),
(CharField,'Char'),
...
)
field_type = models.CharField(max_length=20, choices=FIELD_TYPE_CHOICES, default=CharField)
However, this way I can have the required registration fields to be choosen from a given set, but I only get the fieldtypes as strings, which in turn might lead to complicated data validation.
Example of what i try to achive:
In the admin panel I want the admin to have they choice what kind of data is required (additionally to username and password) at the registration page. The admin might like to ask for (username, password and email) but might also ask for (username, password, email, first_name, last_name, company, department)
Allowing users to customize forms via forms is not that simple, and will probably drag you into more non standard problems... How about trying out a ready made django package that helps building forms?
https://www.djangopackages.com/grids/g/form-builder/