Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django将值更新到OneToOneField字段_Python_Django_Django Models - Fatal编程技术网

Python Django将值更新到OneToOneField字段

Python Django将值更新到OneToOneField字段,python,django,django-models,Python,Django,Django Models,嗨,我有从auth导入的用户对象和名为UserProfile的不同模型,该模型具有用户的bio from django.contrib.auth.models import User class UserProfile(models.Model): user = models.OneToOneField(User) bio=models.TextField(null=True,blank=True, max_length=128) def __unicode__(se

嗨,我有从auth导入的用户对象和名为UserProfile的不同模型,该模型具有用户的bio

from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    bio=models.TextField(null=True,blank=True, max_length=128)

    def __unicode__(self):
        return self.user
由于用户是OneToOneFieldIm正在向其中插入数据

q=User.objects.get(id=1)
>>> <User: test>

q.userprofile_set.create(bio='check')
q=User.objects.get(id=1)
>>> 
q、 userprofile\u set.create(bio='check')
Im获取错误“用户”对象没有属性“userprofile\u set”。我是django的新手。如何插入具有OneToOneField的数据


非常感谢您提供的任何帮助…请提前通知多个字段和Foreignkey字段创建一个对象。您可以在对象上设置属性,但OneToOneFields不能。相反,您可以直接使用object.relation(因为我们知道只有一个)

因此,请尝试
q.userprofile.create(bio=“check”)


(额外提示:每当您想查看python对象的属性时,请使用dir()方法,它将向您显示可用属性的完整列表!)

@Emil..Im收到“RelatedObjectsDoetExist:用户没有用户配置文件。”这是有道理的错误。现在,您正在进行正确的查询,但是您的用户没有配置文件,您需要先创建一个。嘿,您能帮我为这个用户编写查询吗……我只是想为特定用户将数据添加到bio中……感谢mate花费您的时间尝试以下操作:userprofile=userprofile()userprofile.user=q userprofile.bio=“您的文本”userprofile.save()为一对多关系添加_集