扩展Django用户配置文件的新手,需要帮助为数据库中的每个用户创建联系人/电话号码列表吗
我对Django有点陌生,到目前为止,我只是在使用简单的数据库/模型配置。但我开始遇到一些限制。我需要为个人用户添加一个联系人列表,该列表只存储姓名和电话号码的集合。 但是,对于我已经创建的UserProfile模型,除了将整个列表存储为字符串或反序列化的JSON存储在单个字符或文本字段中之外,我看不到其他方法可以做到这一点(糟糕) 例如,假设我必须建模:扩展Django用户配置文件的新手,需要帮助为数据库中的每个用户创建联系人/电话号码列表吗,django,model,Django,Model,我对Django有点陌生,到目前为止,我只是在使用简单的数据库/模型配置。但我开始遇到一些限制。我需要为个人用户添加一个联系人列表,该列表只存储姓名和电话号码的集合。 但是,对于我已经创建的UserProfile模型,除了将整个列表存储为字符串或反序列化的JSON存储在单个字符或文本字段中之外,我看不到其他方法可以做到这一点(糟糕) 例如,假设我必须建模: class UserProfile(models.Model): user = models.OneToOneField(User)
class UserProfile(models.Model):
user = models.OneToOneField(User)
#Persional Informaiton
height = models.CharField(max_length=24, blank=False,null=True)
weight = models.CharField(max_length=24, blank=False,null=True)
birthday = models.CharField(max_length=24, blank=False,null=True)
###THIS IS THE FIELD I NEED TO ADD!!
#Contacts
contact_list = models.SomeKindOfField() #could contain either an array or JSON??
class Meta:
managed = True
db_table = 'user_profiles'
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
还有其他的方法吗?我还想过为每个用户创建一个包含“name”和“phone”列的表,但我开始怀疑这是否会是一场噩梦
我在做一些关于使用models.ForeignKey(用户)的研究,但从未完全掌握它的实现
这里的任何指点都将不胜感激!谢谢。如果您希望用标准的关系数据库方式来实现这一点,有几个选项
联系人
型号和外键添加到用户档案
。这将允许您执行用户配置文件。请联系\u set.all()
。或者,如果您希望多个用户能够共享某些联系人,您可以使用manytomy来代替FK电话号码
属性添加到您的用户配置文件模型中。然后contact\u list=models.ManyToManyField('self')
。见文件:或者,如果您不想担心更多的模型关系,并且正在使用postgres,您可以使用
hstore
,它允许您存储字典字段。有关如何工作的实际文档,请参阅。如果您希望以标准关系数据库的方式执行此操作,有几个选项
联系人
型号和外键添加到用户档案
。这将允许您执行用户配置文件。请联系\u set.all()
。或者,如果您希望多个用户能够共享某些联系人,您可以使用manytomy来代替FK电话号码
属性添加到您的用户配置文件模型中。然后contact\u list=models.ManyToManyField('self')
。见文件:或者,如果您不想担心更多的模型关系,并且正在使用postgres,您可以使用
hstore
,它允许您存储字典字段。请参阅,以获取有关如何工作的实际文档。非常感谢baylee的精彩解释。我能够启动并运行一些东西。我不完全确定这是否是最佳实践?我也不确定这个方案中是否存在任何安全漏洞或瓶颈,但它在我的应用程序中似乎运行良好
models.py
views.py
forms.py
谢谢大家 非常感谢贝利的精彩解释。我能够启动并运行一些东西。我不完全确定这是否是最佳实践?我也不确定这个方案中是否存在任何安全漏洞或瓶颈,但它在我的应用程序中似乎运行良好 models.py views.py forms.py
谢谢大家 @Gman-O,我没有代表对你的回答发表评论,womp。但很高兴你能成功!Re:最佳实践/我的意见是,
管理的
和db_表
属性通常保留为默认值。使用Django 1.5+时,您不再需要UserProfile模型,而是可以。但是,嘿,如果一切正常…@Gman-O,我没有代表对你的答案发表评论,womp。但很高兴你能成功!Re:最佳实践/我的意见是,管理的
和db_表
属性通常保留为默认值。使用Django 1.5+时,您不再需要UserProfile模型,而是可以。但是,如果一切正常的话。。。
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
height = models.CharField(max_length=24, blank=False,null=True)
weight = models.CharField(max_length=24, blank=False,null=True)
birthday = models.CharField(max_length=24, blank=False,null=True)
class Meta:
managed = True
db_table = 'user_profiles'
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
class UserContacts(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=24, blank=False,null=True)
phone = models.CharField(max_length=24, blank=False,null=True)
class Meta:
managed = True
db_table = 'user_contacts'
def add_contact(request):
if request.user.is_authenticated():
update_success = False
form = addContact(request.POST or None)
if request.method == 'POST':
if form.is_valid():
data = form.cleaned_data
contact = form.save(commit=False)
contact.user = request.user
contact.save()
form = addContact() #return blank form so user can continue adding to db
update_success = True
return render_to_response('contact_form.html',
{'update_success':update_success,
'form':form,
},
context_instance=RequestContext(request),
)
else:
return redirect('/accounts/login/')
from django import forms
#Using Crispy forms helper object and bootstrap styling
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, ButtonHolder
from crispy_forms.bootstrap import TabHolder, Tab
#Using Models from "project.account" app
from accounts.models import UserContacts
class addContact(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(addContact, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
TabHolder(
Tab(
'Basic Information',
'name',
'phone',
),
),
ButtonHolder(
Submit('Save', 'Proceed', css_class='btn btn-lg btn-default')
),
)
class Meta:
model = UserContacts
fields = (
'name',
'phone',
)