Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.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-从自定义对象获取值_Python_Django_Django Models_Django Forms_Django Views - Fatal编程技术网

Python Django-从自定义对象获取值

Python Django-从自定义对象获取值,python,django,django-models,django-forms,django-views,Python,Django,Django Models,Django Forms,Django Views,我正在使用Django为用户创建OneToOneField对象,代码如下: class ControlInformation(models.Model): user = models.OneToOneField(User) TURN_ON_OFF = ( ('ON', 'On'), ('OFF', 'Off'), ) AUTO_MANU = ( ('ON', 'On'), ('OFF', 'Off

我正在使用Django为用户创建OneToOneField对象,代码如下:

class ControlInformation(models.Model):
    user = models.OneToOneField(User)

    TURN_ON_OFF = (
        ('ON', 'On'),
        ('OFF', 'Off'),
    )

    AUTO_MANU = (
        ('ON', 'On'),
        ('OFF', 'Off'),
    )

    TEMP_DINNINGROOM = (
        ('HIGH', 'High'),
        ('MEDIUM', 'Medium'),
        ('LOW', 'Low'),
    )

    TEMP_LIVINGROOM = (
        ('HIGH', 'High'),
        ('MEDIUM', 'Medium'),
        ('LOW', 'Low'),
    )

    turn_on_off = models.CharField(max_length=2, choices=TURN_ON_OFF)
    auto_manu = models.CharField(max_length = 2, choices=AUTO_MANU)
    temp_dinningroom = models.CharField(max_length=2, choices=TEMP_DINNINGROOM)
    temp_livingroom = models.CharField(max_length=2, choices=TEMP_LIVINGROOM)


#signal function: if a user is created, add control information to the user    
def create_control_information(sender, instance, created, **kwargs):
    if created:
        ControlInformation.objects.create(user=instance)

post_save.connect(create_control_information, sender=User)
class ControlInformationForm(forms.Form):

    TURN_ON_OFF = (
        ('ON', 'On'),
        ('OFF', 'Off'),
    )

    AUTO_MANU = (
        ('ON', 'On'),
        ('OFF', 'Off'),
    )

    TEMP_DINNINGROOM = (
        ('HIGH', 'High'),
        ('MEDIUM', 'Medium'),
        ('LOW', 'Low'),
    )

    TEMP_LIVINGROOM = (
        ('HIGH', 'High'),
        ('MEDIUM', 'Medium'),
        ('LOW', 'Low'),
    )

    on_off = forms.ChoiceField(label="on_off", choices=TURN_ON_OFF)
    auto_manu = forms.ChoiceField(label="auto_manu", choices=AUTO_MANU)
    temp_dinningroom = forms.ChoiceField(label="temp_dinningroom", choices=TEMP_DINNINGROOM)
    temp_livingroom = forms.ChoiceField(label="temp_livingroom", choices=TEMP_LIVINGROOM)
然后,我用下面的代码为这个对象创建了一个表单:

class ControlInformation(models.Model):
    user = models.OneToOneField(User)

    TURN_ON_OFF = (
        ('ON', 'On'),
        ('OFF', 'Off'),
    )

    AUTO_MANU = (
        ('ON', 'On'),
        ('OFF', 'Off'),
    )

    TEMP_DINNINGROOM = (
        ('HIGH', 'High'),
        ('MEDIUM', 'Medium'),
        ('LOW', 'Low'),
    )

    TEMP_LIVINGROOM = (
        ('HIGH', 'High'),
        ('MEDIUM', 'Medium'),
        ('LOW', 'Low'),
    )

    turn_on_off = models.CharField(max_length=2, choices=TURN_ON_OFF)
    auto_manu = models.CharField(max_length = 2, choices=AUTO_MANU)
    temp_dinningroom = models.CharField(max_length=2, choices=TEMP_DINNINGROOM)
    temp_livingroom = models.CharField(max_length=2, choices=TEMP_LIVINGROOM)


#signal function: if a user is created, add control information to the user    
def create_control_information(sender, instance, created, **kwargs):
    if created:
        ControlInformation.objects.create(user=instance)

post_save.connect(create_control_information, sender=User)
class ControlInformationForm(forms.Form):

    TURN_ON_OFF = (
        ('ON', 'On'),
        ('OFF', 'Off'),
    )

    AUTO_MANU = (
        ('ON', 'On'),
        ('OFF', 'Off'),
    )

    TEMP_DINNINGROOM = (
        ('HIGH', 'High'),
        ('MEDIUM', 'Medium'),
        ('LOW', 'Low'),
    )

    TEMP_LIVINGROOM = (
        ('HIGH', 'High'),
        ('MEDIUM', 'Medium'),
        ('LOW', 'Low'),
    )

    on_off = forms.ChoiceField(label="on_off", choices=TURN_ON_OFF)
    auto_manu = forms.ChoiceField(label="auto_manu", choices=AUTO_MANU)
    temp_dinningroom = forms.ChoiceField(label="temp_dinningroom", choices=TEMP_DINNINGROOM)
    temp_livingroom = forms.ChoiceField(label="temp_livingroom", choices=TEMP_LIVINGROOM)
最后,我用

ControlInformation = request.user.get_profile()
form=ControlInformationForm(request.POST)

在views.py中获取ControlInformation对象的值,但它不起作用(错误:“UserProfile”对象没有属性“turn\u on\u off”)。我认为出现问题是因为我使用了
request.user.get\u profile()
。如何修改它以获取ControlInformation对象的值,然后修改并保存它?

而不是
ControlInformation=request.user.get\u profile()
使用:

instance = ControlInformation.objects.get(user=request.user)
更多提示:

  • 使用
    ModelForm
    从模型自动创建表单

  • 您可以使用
    布尔字段来代替开/关

您可以对两个字段使用一个查找:

  TEMP = (
        ('HIGH', 'High'),
        ('MEDIUM', 'Medium'),
        ('LOW', 'Low'),
  )

享受Django

您需要包含更多关于views.py的信息,因为错误引用的是问题中未定义的
UserProfile
对象。另外,您是否在设置中将
appname.ControlInformation
设置为
AUTH\u PROFILE\u MODULE