Multi user profile creation function - django-models

I'm django learner and I'm trying to design multiple userprofile system.
I recently saw the create_profile function for single userProfile. How can I redesign it to multi user profile creation function?
def create_profile(sender,**kwargs):
if kwargs["created"]:
user_profile=Student.objects.create(user=kwargs["instance"])
post_save.connect(create_profile,sender=User)

Have two models for each profile type. If you want, have them inherit from a base model but you don't need to.
class Student(models.Model):
user = models.ForeignKey(User)
# more fields
class Master(models.Model):
user = models.ForeignKey(User)
# more fields
myuser = User.objects.get(...)
student = Student.objects.create(user=myuser)
master = Master.objects.create(user=myuse

Related

Appending data to User Model in Django

Here is how my model looks like:
class Maintab(models.Model):
email = models.CharField(max_length=255)
username = models.CharField(max_length=255)
password = models.CharField(max_length=255)
lab_name = models.CharField(max_length=255)
type = models.CharField(max_length=255)
def __str__(self):
return self.username
I want to append the User table that looks after the login, with the username and password of my Maintab model. If only the username and password of the user table could get updated.
Or if I could use Maintab as my Custom UserModel (but it would not have attributes like is_active, is_staff, etc)
Seems like creating your own user models by subclassing the existing user model is the right solution as it enables you to use the existing django code while also adding your own user attributes.
Here is a link to the Django documentation where they describe how to do this:
https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project

Extending the typer of user the most simple way in Django

I want to create two types of user in Django in the most simple way.
I want to use class AbstractBaseUser
class BaseUser(AbstractBaseUser):
email = models.EmailField(max_length=254, unique=True)
class Service_provider(BaseUser):
company = models.CharField(max_length=140);
def __unicode__(self):
return self.company
class Customer(BaseUser):
name = models.CharField(max_length=140);
def __unicode__(self):
return self.name
I don't know how to pass this two user to the user model without applying any complicated change in the auth model.
Is it possible?
No. Django's built-in auth framework does not support more than one user model. You will need to write your own auth framework from scratch.

Multiple references to the same model in another model in Django

Hi I have a lot of users in my system who are classified into different types. I want to store the address details of all those users. For instance the user could be a student, a school or a franchisee. All the users here could have an address information associated with them.
from django.db import models
from django.contrib.auth.Models import User
class Address(models.Model):
user = models.OneToOneField(User)
address = models.TextField()
class Student(models.Model):
user_id = models.ForeignKey(User)
address = models.ForeignKey(Address)
class School(models.Model):
user_id = models.ForeignKey(User)
address = models.ForeignKey(Address)
contact_person_name = models.CharField(max_length=50)
In this scenario there are 2 references to the User model - one through user_id and another through address.user though they should be referring to the same instance. Is it a bad practice to have duplicate references?
I thought of leaving out the 'user' foreignkey in Address, but I think that the address can't exist without a user. How to better model this?
As you already mentioned in question duplication of same field in
a model is not a good Idea.
If these are your actual models, I would suggest you using abstract
models:
from django.db import models
from django.contrib.auth.Models import User
class Profile(models.Model):
user = models.OneToOneField(User, related_name="%(app_label)s_%(class)s_related")
address = models.TextField()
class Meta:
abstract = True
class Student(Profile):
pass
class School(Profile):
contact_person_name = models.CharField(max_length=50)
This will generate two tables: students, schools with fields
user, address and user, address, contact_person_name
respectively.

How can I create an inherited object in the admin from an existing base model instance?

I have the following
class Employee(User):
emplorateID=models.OneToOneField(Code)
business=models.ForeignKey(Business)
but I have existing Users that I'd like to select in the EmployeeAdmin and enter the additional field data for. I don't see how I would customize the ModelAdmin to handle creating a new Employee from an existing User (effectively a new row in the Employee table for referencing the existing User entry)
Inheritance isn't what you want in this case; you just need another model with a OneToOneField pointing back.
Try this:
from django.contrib.auth.models import User
from django.db import models
class Employee(models.Model):
user = models.OneToOneField(User, related_name='employee')
emplorateID = models.OneToOneField(Code)
business = models.ForeignKey(Business)
It should JustWork™.
I don't know the form answer, but here is the code answer:
employee = Employee(user_ptr=existing_user,
emplorateID=something,
business=somebusiness)
employee.save_base(raw=True)

Django form on the model

I have Guest model in my app:
class Guest(models.Model):
event = models.ForeignKey(Event, related_name='guests')
contact = models.ForeignKey(Contact, related_name='guests')
attending_status = models.CharField(max_length=32, choices=ATTENDING_CHOICES, default='no_rsvp')
number_of_guests = models.SmallIntegerField(default=0)
comment = models.CharField(max_length=255, blank=True, default='')
updated = models.DateTimeField(blank=True, null=True)
Event and Contact I fill up by myself in Admin when creating a guest. On the site all I want is a guest to fill up the form where he refreshes his attending status, points out number of guests and leaves a comment.
class RSVPForm(forms.Form):
attending = forms.ChoiceField(choices=VISIBLE_ATTENDING_CHOICES, initial='yes', widget=forms.RadioSelect)
number_of_guests = forms.IntegerField(initial=0)
comment = forms.CharField(max_length=255, required=False, widget=forms.Textarea)
How can I save the changes to the Guest model instance? How can I access the guest's id when saving the changes to his profile?
You are probably looking for Django's ModelForms. Instead of subclassing forms.Form in your RSVPForm class, you should subclass forms.ModelForm. Then you can use the features of the model form class to help you achieve what you want (hide some fields, etc). An example is below:
class RSVPForm(forms.ModelForm):
class Meta:
model = Guest
fields = ('attending_status', 'number_of_guests', 'comment')
This will do essentially what you want, but you will need to remember to provide an instance keyword argument to the form constructor in your view function. This argument is the instance of the Guest model you will be updating with the form.
form = RSVPForm(instance=guest_object)
Now calls to the form.save() method will automatically save the new data to this Guest object instance. You just need to make sure that you always pass the instance keyword argument, even when using request.POST:
form = RSVPForm(request.POST, instance=guest_object)

Resources