how to create django multiple user types - django

I am working on a project where I need to have 3 types of Users.
Admin
Vendor
Customer
My first approach to this problem was to define all models by sub-classing the AbstractUser model
class Customer(AbstractUser):
pass
class Vendor(AbstractUser):
pass
so the question is do i need to craete a UsermManager for every usertype model ?

As I said into a comment, the best way to achieve what you want is to use the Multi Table Inheritance paradigm in Django. It defines OneToOne relations underneath so you could do it on your own.
Extending the User model is also explained HERE (official Django documentation) and it is done the way I told you (with OneToOne relations).

Related

implementing multiple profiles with django-all-auth

Django noob here - I was recently pointed to django-all-auth for registering/handling users, which I'm finding awesome in its near instant setup.
However I'm stumbling at trying to implement multiple user profile models. In reading other answers I've found this to be the closest answer thus far, but not really what I need.
When coding my own basic registration the user would select an account type (basic, pro, elite for example - each being their own profile model). Depending on the link selected the signup form would display both the generic User registration form as well as the profile form of the type chosen by the user.
I know I can go so far as to completely customize all-auth and make something like this work, but I'm hoping to be pointed in a direction that involves less wrecking of the original app. I've thought about having user redirected after signup to choose a profile type, but that seems to be a lot of extra steps.
Thanks for the help!
To extend the basic user class, just subclass AbstractUser. You can find that in the docs. With this you can add the fields your basic user is missing.
Now, you want several types of profiles with different fields, or perhaps the same fields but adding new fields every time.
You can create something like:
class ProfileBase(models.Model):
user=models.OneToOneField(User)
class ProfilePro(ProfileBase):
pro_field=models.SomeField(Foo)
#You can extend ProfilePro here if you want it to include the pro_field
class ProfileElite(ProfileBase):
elite_field=models.someField(Bar)
Once you have these models creating the forms should be easy.
Be aware, when you subclass this way django creates 1 table per model, including on the subclass table only the new fields. This makes necessary a join per level of inheritance so try not to abuse that.
There is a second way to use inheritance:
class ProfileBase(models.Model):
user=models.OneToOneField(User)
class Meta:
abstract=True
If you define the base class as abstract you won't have a table for it, so you can't create objects for that model, but each of your subclasses will be on it's own table. If you do it this way you may need extra logic to handle cases when user changes of type of profile (i.e. went from pro to elite).
To keep this separated from django-allauth, just finish the registration (create a form and in your settings define ACCOUNT_SIGNUP_FORM_CLASS to override all-auth default with your basic info + pick a profile type form) and once your user is logged in, redirect them to their profile to finish the process.

Django. Many forms have OneToOne to one specific model. Forms should always be saved with these relations. How to implement efficiently?

I have many models (Customer, Seller, Product etc.), each of them has a set of images (Gallery, OneToOne relationship).
Also, some models (Customer, Seller, Moderator, Administrator) has OneToOne with User that is used for storing credentials.
I want to create and update these related models together.
Of course, simplest way is to use a class-based view and override get, post and form_valid methods. But there are many models having Gallery and User and I intend to follow DRY and code reuse principles and not to alter each view in same way.
Ideally, solution is sophisticated form (or form set) and using of standard class-based views without method overriding. Other option is different form and class-based view mixin.
I thought few hours about mixins but didn't came up with solution. Now I'm trying to do something with some kind of form sets.
In the case of the gallery is recommended to create an app only for that purpose.
On the case of the user the implementation Here's my recommendation:
1 - Create a base User class with all the core functionality your looking for.
2 - Extend the User class for all the other cases with the functionality your looking for.

django registering site with admin?

For a given model :
class SignUp:
....
I have seen two ways of registering the model to admin in few tutorials.
one is simply using
admin.site.register(SignUp)
and other is:
class signUpAdmin(admin.ModelAdmin):
class Meta:
model=SignUp
admin.site.register(SignUp, SignUpAdmin)
If I am correct, the second way is handy when we want to customize the admin (display, sorted order etc). but if one is not interested in that, then both serve the same purpose correct?
From the docs:
...you can register the model class without providing a ModelAdmin
description.
So admin.site.register(SignUp) would be the same as,
class signUpAdmin(admin.ModelAdmin):
pass
admin.site.register(SignUp, signUpAdmin)
Yes, that's correct.
The first one is the 'out-of-the-box' solution, while the second one is needed when you want to do more fancy stuff (for example editing a model that has a 1 to n relation).

Creating Model Relationships Via Forms

What is the defacto way of creating model relationships in Django via frontend forms.
For example a user signs up for service using a form, they start a quote.
In getting a quote they can select and add products to their quote specifying variable such as sizes in this process.
This is modelled with relevant User, Quote, Product models and relevant relationships.
I am trying to work out the best way that these are linked together by frontend forms and views.
Would I load into the quote form a hidden field for the related user_id for example, which I can then process manually to form the one-to-many relationship.
I am just wondering if this is something accounted for within forms or if I have to manually create the forms to achieve my goal.
This is one of the more complicated things to try and achieve but there are several things in Django which will help you.
You're going to need a ManyToMany field on the Quote model to link the Products to it.
This can be displayed in forms simply via a ModelMultipleChoiceField:
https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelmultiplechoicefield
... which is just renders a basic multiple select list of existing products.
The interface you want probably looks more like an inline formset however. The complication here is that they are designed for ForeignKey relations rather than ManyToMany.
Under the covers, a ManyToMany relation is actually just two ForeignKey relations, via an intermediate 'through' model. We can exploit this to build an inline formset on the through model, see this answer:
https://stackoverflow.com/a/10999074/202168
You'll note the caveat in that answer, the inline rows won't know which Quote they belong to unless you override some code.
You may like to look at some helper apps which provide custom widgets for ManyToMany fields:
https://code.google.com/p/django-ajax-filtered-fields/
http://django-autocomplete-light.readthedocs.org/en/latest/

How to program the EDIT and DELETE features in Model Form Django

I have the Models with fields. basically i want to do the CRUD opertions like in admin.I have used
Class Meta
model = Modelname
Now my create part is done by defining function create_from.
I want to know that do i need to create separate functions to EDIT , DELETE , READ all Model data. Or i can use buildin admin functions for that. because all the functionality is already on admin side.
See https://docs.djangoproject.com/en/1.3/ref/class-based-views/ - especially the Editing Mixins on that page.