Search code examples
djangomodeldjango-users

extending django usermodel


i am trying to create a signup form for my django app. for this i have extended the user model. This is my Forms.py

from contact.models import register
from django import forms
from django.contrib import auth
class registerForm(forms.ModelForm):

    class Meta:
    model=register
        fields = ('latitude', 'longitude', 'status')
    class Meta:
        model = auth.models.User # this gives me the User fields
        fields = ('username', 'first_name', 'last_name', 'email')

and this is my model.py

from django.db import models
from django.contrib.auth.models import User
STATUS_CHOICES = (
('Online', 'Online.'),
('Busy', 'Busy.'),
('AppearOffline', 'AppearOffline.'),)
 class register(models.Model): 

    user = models.ForeignKey('auth.User', unique = True) 
    latitude = models.DecimalField(max_digits=8, decimal_places=6)
    longitude = models.DecimalField(max_digits=8, decimal_places=6)
status = models.CharField(max_length=8,choices=STATUS_CHOICES, blank= True, null=True)

i dont know where i am making a mistake. the users passwords are not accepted at the login and the latitude and logitude are not saved against the created user user. i am fiarly new to django and dont know what to do any body have any solution .?


Solution

  • To inherit from django's user model, you have to something like this:

    from django.contrib.auth.models import User
    class MyUserClass(User):
        #your fields go here
    

    But another way to extend the user model, which would be suitable for you, if you just want to store latitude/longitude is to create an user profile that is realted to the user model. See here: http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/ You can then find a solution where you can administrate the related profile through an InlineAdmin in a UserAdmin sub class!
    See this for extending the admin: http://pyxx.org/2008/08/18/how-to-extend-user-model-in-django-and-enable-new-fields-in-newforms-admin/
    And this for automatically creating a user profile upon creation of a new user: Django: UserProfile with Unique Foreign Key in Django Admin