扩展 Django 用户模型
我正在尝试为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要继承 django 的用户模型,您必须这样做:
但是,如果您只想存储纬度/经度,则扩展用户模型的另一种方法(这将适合您)是创建一个与用户模型。请参阅此处: 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:
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
继承“User”模型很好当你需要自定义时操作用户对象的方法。要存储其他信息,最好的方法是为此使用单独的模型类。
如果您想在管理面板中内联新字段,您需要重新注册用户模型,如下所示:
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: