How to make Django custom user model an abstract class - python

I have created my own custom user model in django and specified the same in settings.py :
AUTH_USER_MODEL = 'userprofile.User'
After this, I created User as an abstract class since I wanted to have two separate classes derived from this class namely - Vendor and Customer. This will create two separate database tables.
The problem occurs when I created another class - ProductReview. This class needs a foreign key to the user who added the review and this user can be either a Vendor or a Customer. I am not sure how to put this foreign key constraint here, because settings.AUTH_USER_MODEL will not work, as User is an abstract class.
Following is the class structure:
class UserManager(BaseUserManager):
class User(PermissionsMixin, AbstractBaseUser):
class Meta:
abstract = True
class Vendor(User):
class Customer(User):
class ProductReview(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, related_name='reviews', null=True, blank=True)
Edit - I went ahead with three different tables for each of the models.

For your case I would consider changing my modeling. This how I would do it:
Leave the default User as the auth user model for Django.
Create a Vendor and a Customer class with one-to-one relationship to the User model in order to extend it.
In this way, a you can have a ProductReview to reference a User, who could be either a Vendor or a Customer, or you can reference Customers only if you wish.
See more on Django documentation: Extending the User model.

The declaration of user field in ProductReview model class will map with the USER model. To define whether it is a Vendor or a Customer you need to declare another field probably integer field with mapping declared as
TYPE_OF_USER_MAPPING = {
1: 'Vendor',
2: 'Customer'
}
and then create a query to check in which model Vendor or Customer the original USER is.

Related

Django Model - check validity of the models

I am new to Django - ORM and facing issues with creating relations .
The db is SQLite .
I am trying to solve following problem present -
A User will add different Skills to his/her profile .S/He can also
create new skills which were earlier not present and can add to the
profile .
Other users can upvote the skills on user profile - this should be
recorded in Database too .
Uptill not I have created following model
#import from django models module
from django.db import models
# This class will more or less map to a table in the database and defines skills at the application level
class Skill (models.Model):
# this defines a required name that cannot be more than 100 characters.
skill_name = models.CharField(max_length=100,unique=True)
class Meta:
app_label = "wantedly_webapp"
# This class will more or less map to a table in the database and defines the many to many relationship between user-skill, this is our intermediate model
class UserSkill(models.Model):
""" A Model for representing skill in user profile """
unique_together = (('user', 'skill_item'),)
user = models.ForeignKey('UserProfile',on_delete=models.CASCADE)
# Define a foreign key relating this model to the Skill model.
# The parent user will be able to access it's skills with the related_name
# 'all_user_skills'. When a parent is deleted, this will be deleted as well.
skill_item = models.ForeignKey(
Skill,
related_name='all_user_skills', on_delete=models.CASCADE
)
def __str__(self):
"""Return a human readable representation of the model instance."""
return "{}".format(self.skill_item.skill_name)
# this class adds a Many to Many field in existing django-rest auth UserProfile class for user and his/her skills
class UserProfile(models.Model):
user = models.OneToOneField('auth.User',unique=True,on_delete=models.CASCADE)
user_skills = models.ManyToManyField(
Skill,
through='UserSkill',
through_fields=('user','skill_item')
)
class UserSkillUpvotes(models.Model):
unique_together = (('user_skill', 'upvote_by'),)
user_skill = models.OneToOneField('UserSkill',unique=True,on_delete=models.CASCADE)
upvote_by = models.OneToOneField('auth.User',unique=True,on_delete=models.CASCADE)
I have created models now , next I need help on how to test this and update if anything is required for operations like
Adding new skill
Adding existing skill to user profile
Saving up-votes of skill on current user profile by other users etc

Two level extending of User in Django

I'm working on a Django project. There are two types of users. First type - Customer (UserProfile) is an extended built-in User because everybody has to have stored it's telephone, address etc. But there is another type of User called Translator. The translator has all permissions and attributes like Customer (UserProfile) and some new attributes - Languages,Prices etc.
I've chosen a probably most common way to handle a UserProfile:
class UserProfile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,related_name='userprofile')
telephone = models.CharField(max_length=40,null=True)
...
Now, I want to create Customer profile. I can't decide whether I should extend User (every user has already UserProfile) or to extend UserProfile itself.
So the option A is:
class TranslatorUserProfile(models.Model):
user_profile = models.OneToOneField(User)
languages ...
...
And option B is:
class TranslatorUserProfile(models.Model):
user_profile = models.OneToOneField(UserProfile)
languages ...
...
Translator will be able to access more pages than regular User which is a UserProfile and will be of course more attributes.
Is there some pattern? What should I do?
Logically, your translator extends the common user. In order to keep the db tables of both separate, you can make both subclass an abstract base model that defines their common attributes:
class AbstractUserProfile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,related_name='%(class)s')
telephone = models.CharField(max_length=40,null=True)
...
class Meta:
abstract = True
class UserProfile(AbstractUserProfile):
pass
class TranslatorUserProfile(AbstractUserProfile):
languages = ...
I have such a structure working fine in a project at work.

Django M2M Through extra fields with multiple models

I'm trying to figure out the best way to set up the following django model (genericised for security reasons).
ThingA:
User(M2M through "UserRelation")
ThingB:
User(M2M through "UserRelation")
ThingC:
User(M2M through "UserRelation")
User:
Login_name
UserRelation:
User (foreginkey)
Thing (foreignkey) #is this generic to any of the above "things"
Privilege
I understand using "through" between two distinct models, but I'm not sure how to apply this to multiple models. Would I define a foreignkey for each of the "Thing" models in my UserRelation Model?
It looks like you are trying to setup a generic many-to-many relationship. There is a dedicated django app that you can be use for this purpose: django-gm2m
Here is how to use it in your generic case:
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from gm2m import GM2MField
class ThingA(models.Model):
pass
class ThingB(models.Model):
pass
class ThingC(models.Model):
pass
class User(models.Model):
login_name = models.CharField(max_length=255)
things = GM2MField(through='UserRelation')
class UserRelation(models.Model):
user = models.ForeignKey(User)
thing = GenericForeignKey(ct_field='thing_ct', fk_field='thing_fk')
thing_ct = models.ForeignKey(ContentType)
thing_fk = models.CharField(max_length=255)
privilege = models.CharField(max_length=1)
You can now access all the things for a given user and all the User instances for a given 'thing', as well as the privilege attribute for each UserRelation instance.
This will additionally provide you with a handful of benefits (reverse relations, prefetching, etc.) you may need. A GM2MField basically behaves exactly like a django ManyToManyField.
Disclaimer: I am the author of django-gm2m

django abstract models versus regular inheritance

Besides the syntax, what's the difference between using a django abstract model and using plain Python inheritance with django models? Pros and cons?
UPDATE: I think my question was misunderstood and I received responses for the difference between an abstract model and a class that inherits from django.db.models.Model. I actually want to know the difference between a model class that inherits from a django abstract class (Meta: abstract = True) and a plain Python class that inherits from say, 'object' (and not models.Model).
Here is an example:
class User(object):
first_name = models.CharField(..
def get_username(self):
return self.username
class User(models.Model):
first_name = models.CharField(...
def get_username(self):
return self.username
class Meta:
abstract = True
class Employee(User):
title = models.CharField(...
I actually want to know the difference between a model class that
inherits from a django abstract class (Meta: abstract = True) and a
plain Python class that inherits from say, 'object' (and not
models.Model).
Django will only generate tables for subclasses of models.Model, so the former...
class User(models.Model):
first_name = models.CharField(max_length=255)
def get_username(self):
return self.username
class Meta:
abstract = True
class Employee(User):
title = models.CharField(max_length=255)
...will cause a single table to be generated, along the lines of...
CREATE TABLE myapp_employee
(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
title VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
...whereas the latter...
class User(object):
first_name = models.CharField(max_length=255)
def get_username(self):
return self.username
class Employee(User):
title = models.CharField(max_length=255)
...won't cause any tables to be generated.
You could use multiple inheritance to do something like this...
class User(object):
first_name = models.CharField(max_length=255)
def get_username(self):
return self.username
class Employee(User, models.Model):
title = models.CharField(max_length=255)
...which would create a table, but it will ignore the fields defined in the User class, so you'll end up with a table like this...
CREATE TABLE myapp_employee
(
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
An abstract model creates a table with the entire set of columns for each subchild, whereas using "plain" Python inheritance creates a set of linked tables (aka "multi-table inheritance"). Consider the case in which you have two models:
class Vehicle(models.Model):
num_wheels = models.PositiveIntegerField()
class Car(Vehicle):
make = models.CharField(…)
year = models.PositiveIntegerField()
If Vehicle is an abstract model, you'll have a single table:
app_car:
| id | num_wheels | make | year
However, if you use plain Python inheritance, you'll have two tables:
app_vehicle:
| id | num_wheels
app_car:
| id | vehicle_id | make | model
Where vehicle_id is a link to a row in app_vehicle that would also have the number of wheels for the car.
Now, Django will put this together nicely in object form so you can access num_wheels as an attribute on Car, but the underlying representation in the database will be different.
Update
To address your updated question, the difference between inheriting from a Django abstract class and inheriting from Python's object is that the former is treated as a database object (so tables for it are synced to the database) and it has the behavior of a Model. Inheriting from a plain Python object gives the class (and its subclasses) none of those qualities.
The main difference is how the databases tables for the models are created.
If you use inheritance without abstract = True Django will create a separate table for both the parent and the child model which hold the fields defined in each model.
If you use abstract = True for the base class Django will only create a table for the classes that inherit from the base class - no matter if the fields are defined in the base class or the inheriting class.
Pros and cons depend on the architecture of your application.
Given the following example models:
class Publishable(models.Model):
title = models.CharField(...)
date = models.DateField(....)
class Meta:
# abstract = True
class BlogEntry(Publishable):
text = models.TextField()
class Image(Publishable):
image = models.ImageField(...)
If the Publishable class is not abstract Django will create a table for publishables with the columns title and date and separate tables for BlogEntry and Image. The advantage of this solution would be that you are able to query across all publishables for fields defined in the base model, no matter if they are blog entries or images. But therefore Django will have to do joins if you e.g. do queries for images...
If making Publishable abstract = True Django will not create a table for Publishable, but only for blog entries and images, containing all fields (also the inherited ones). This would be handy because no joins would be needed to an operation such as get.
Also see Django's documentation on model inheritance.
Just wanted to add something which I haven't seen in other answers.
Unlike with python classes, field name hiding is not permited with model inheritance.
For example, I have experimented issues with an use case as follows:
I had a model inheriting from django's auth PermissionMixin:
class PermissionsMixin(models.Model):
"""
A mixin class that adds the fields and methods necessary to support
Django's Group and Permission model using the ModelBackend.
"""
is_superuser = models.BooleanField(_('superuser status'), default=False,
help_text=_('Designates that this user has all permissions without '
'explicitly assigning them.'))
groups = models.ManyToManyField(Group, verbose_name=_('groups'),
blank=True, help_text=_('The groups this user belongs to. A user will '
'get all permissions granted to each of '
'his/her group.'))
user_permissions = models.ManyToManyField(Permission,
verbose_name=_('user permissions'), blank=True,
help_text='Specific permissions for this user.')
class Meta:
abstract = True
# ...
Then I had my mixin which among other things I wanted it to override the related_name of the groups field. So it was more or less like this:
class WithManagedGroupMixin(object):
groups = models.ManyToManyField(Group, verbose_name=_('groups'),
related_name="%(app_label)s_%(class)s",
blank=True, help_text=_('The groups this user belongs to. A user will '
'get all permissions granted to each of '
'his/her group.'))
I was using this 2 mixins as follows:
class Member(PermissionMixin, WithManagedGroupMixin):
pass
So yeah, I expected this to work but it didn't.
But the issue was more serious because the error I was getting wasn't pointing to the models at all, I had no idea of what was going wrong.
While trying to solve this I randomly decided to change my mixin and convert it to an abstract model mixin. The error changed to this:
django.core.exceptions.FieldError: Local field 'groups' in class 'Member' clashes with field of similar name from base class 'PermissionMixin'
As you can see, this error does explain what is going on.
This was a huge difference, in my opinion :)
The main difference is when you inherit the User class. One version will behave like a simple class, and the other will behave like a Django modeel.
If you inherit the base "object" version, your Employee class will just be a standard class, and first_name won't become part of a database table. You can't create a form or use any other Django features with it.
If you inherit the models.Model version, your Employee class will have all the methods of a Django Model, and it will inherit the first_name field as a database field that can be used in a form.
According to the documentation, an Abstract Model "provides a way to factor out common information at the Python level, whilst still only creating one database table per child model at the database level."
I will prefer the abstract class in most of the cases because it does not create a separate table and the ORM does not need to create joins in the database. And using abstract class is pretty simple in Django
class Vehicle(models.Model):
title = models.CharField(...)
Name = models.CharField(....)
class Meta:
abstract = True
class Car(Vehicle):
color = models.CharField()
class Bike(Vehicle):
feul_average = models.IntegerField(...)

Django: "UserProfileRole.userProfile" must be a "UserProfile" instance

I'm having a strange riddle to solve:
I extended my django-1.4 user-objects with a UserProfile, as described at https://docs.djangoproject.com/en/dev/topics/auth/ and wanted to implement project-specific roles. So my models look like the following:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
projects = models.ManyToManyField(Project, through='UserProjectRole')
[...]
class UserProjectRole(models.Model):
userProfile = models.ForeignKey(UserProfile)
project = models.ForeignKey(Project)
group = models.ForeignKey(Group)
[...]
I needed to pass a css-class, so I created a model-form for UserProjectRole and implemented the userProfile-Field with a widget:
class ProjectRoleForm(forms.ModelForm):
userProfile = forms.ModelMultipleChoiceField(label='Users',
queryset=UserProfile.objects.all(),
widget=forms.SelectMultiple(attrs={'class': 'select-multiple'}))
class Meta:
model = UserProjectRole
The form is presented correctly, however, it's crashing during save-process with the following error
Cannot assign "[<UserProfile: MyUser>]": "UserProjectRole.userProfile" must be a "UserProfile" instance.
Does anyone have an idea?
My guess is it's because you are using a forms.SelectMultiple widget. Which gives you a list of UserProfile instances ( [<UserProfile: MyUser>] ) and not a single UserProfile instance which is of course required to set on a ForeignKey field (UserProjectRole.userProfile). Thus I suggest to try using a forms.Select widget instead.

Categories

Resources