سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top