views:

61

answers:

2

Hi 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 .?

A: 

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: http://stackoverflow.com/questions/2813189/django-userprofile-with-unique-foreign-key-in-django-admin

lazerscience
Why is using the UserProfile way the best? This is the way I do it currently, but I keep telling myself that it would be better if I did it via inheritance. The blog that James Bennett wrote on extending the user model is really old. And Django has changed significantly since then. So I guess my question is: what's wrong with doing it via inheritance? Are there any problems? (Django 1.2+)
b14ck
I do not think it's wrong doing it via inheritance! It depends on what you want to achieve and adding a user profile is the offically recommended way and I guess if the thing is just adding two fields itS totally ok!
lazerscience
thanks for the help... i did it in this way but got another problem that only one password field is shown on the form. and a link saying "change password form" but cant even access that link it gives me a page not found error. and the password is not saved in DB when i check it from django admin any help thanks
MajorGeek
Did you unregister the original UserAdmin and register yours that inherits from the original one? (admin.site.unregister(User);admin.site.register(User, MyUserAdmin)
lazerscience
+1  A: 

Inheritance of "User" model good when you need custom methods to manipulate with user objects. To store additional information, best way is to use separate model class for this. If you want to inline of your new fields in admin panel, you need to re register User model like this:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from MyApp.models import MyUserProfile

class MyUserProfileInline(admin.TabularInline):
    model = MyUserProfile
    fk_name = 'user'
    max_num = 1

class NewUserAdmin(UserAdmin):
    inlines = [MyUserProfileInline, ]

# Then reregister
admin.site.unregister(User)
admin.site.register(User, NewUserAdmin)
Saff
See related question: http://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django
Török Gábor