Defining two different extensions for a user model

class CustomerProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    gender = models.CharField(max_length=1,blank=True)
    zip_code = models.IntegerField()

class StoreProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    phone_number = models.IntegerField()

I would like to be able to log in / authenticate the user either as a "store" or as a "client."

  • Is there any way to make this work with the above model?

  • I will also look at the decorator @login_requiredto distinguish between a registered store and a customer. Any tips on how to proceed?

+3
source share
3 answers

I would like to be able to log in / authenticate the user either as a "store" or as a "client."

Is there any way to make this work with the model above?

Yes, but.

http://docs.djangoproject.com/en/1.2/topics/auth/#storing-additional-information-about-users

If you want to use automatic functions, you get one (one) object of the profile class associated with the user.

, . AUTH_PROFILE_MODULE get_profile() a User.

try:
    CustomerProfile.objects.get( user=request.user )
except CustomerProfile.DoesNotExist:
    # hmmm.  Must be a Store, not a Customer.

, " ".

@login_required, . , ?

  • save() , . ClientProfile , CustomerProfile.save() StoreProfile , .

    . .

  • , . , , uber.

    @customer_required @store_required. , @login_required, , . customer_required CustomerProfile . store_required StoreProfile .


http://docs.djangoproject.com/en/1.2/topics/auth/#groups

, , Django. , - .

"" .

Django Group ( "", "" ) .

@store_required @customer_required .

+2

AFAIK . :

class UserProfile(models.Model):
    user = ForeignKey(User, unique=True)
    store = OneToOneField(Store, blank=True, null=True)
    customer = OneToOneField(Customer, blank=True, null=True)

store customer .

+2

, - .

One way to approach this is to use the login_required logic, but also assign permissions to authenticate Django to users when they are created - one for the store and one for the client. Otherwise, you can simply write a utility function to determine their membership based on their presence in the external table. For more information see http://docs.djangoproject.com/en/dev/topics/auth/

0
source

All Articles