扩展 Django 用户模型

发布于 2024-09-04 14:21:20 字数 1082 浏览 4 评论 0原文

我正在尝试为我的 django 应用程序创建一个注册表单。为此我扩展了用户模型。这是我的 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')

这是我的 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)

我不知道我在哪里犯了错误。登录时不接受用户密码 并且不会针对创建的用户 user 保存纬度和经度。 我是 django 的新手,不知道该怎么做 任何机构有任何解决方案吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘑菇王子 2024-09-11 14:21:20

要继承 django 的用户模型,您必须这样做:

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

但是,如果您只想存储纬度/经度,则扩展用户模型的另一种方法(这将适合您)是创建一个与用户模型。请参阅此处: http:// www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/
然后,您可以找到一个解决方案,您可以通过 UserAdmin 子类中的 InlineAdmin 来管理相关配置文件!
请参阅此以扩展管理: http://pyxx.org/2008/08/18/how-to-extend-user-model-in-django-and-enable-new-fields-in- newforms-admin/
这用于在创建新用户时自动创建用户配置文件: Django:Django 管理中具有唯一外键的用户配置文件

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

苦行僧 2024-09-11 14:21:20

继承“User”模型很好当你需要自定义时操作用户对象的方法。要存储其他信息,最好的方法是为此使用单独的模型类
如果您想在管理面板中内联新字段,您需要重新注册用户模型,如下所示:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文