Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango add new Model and Form in a new app
    text
    copied!<p>i am using Django and trying to add a new model which can only be accessed after user login. Firstly, i built a model class of UserProfile in an app, using</p> <pre><code>def create_user_profile(sender, instance, created, **kwargs): if created: UserProfile.objects.create(user=instance) post_save.connect(create_user_profile, sender=User) </code></pre> <p>to sync the model into database, it works well by calling <code>UserProfile = request.user.get_profile()</code>, then i built Anther model class called "ControlInformation" in ANOTHER app (called "control"), using</p> <pre><code>def create_control_information(sender, instance, created, **kwargs): if created: ControlInformation.objects.create(user=instance) post_save.connect(create_control_information, sender=User) </code></pre> <p>to sync this model, but there are some problems to use information in this model by calling <code>Current_Status = request.user.get_profile()</code> , which is "'UserProfile' object has no attribute 'turn_on_off'" when calling <code>on_off = Current_Status.turn_on_off</code> in views.py.</p> <p>Am i right when building this one on one model in another app? Or is there any other problem?</p> <p>Edit: my ControlInformation model is like this:</p> <pre><code>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=1, choices=TURN_ON_OFF) auto_manu = models.CharField(max_length = 1, choices=AUTO_MANU) temp_dinningroom = models.CharField(max_length=1, choices=TEMP_DINNINGROOM) temp_livingroom = models.CharField(max_length=1, 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) </code></pre> <p>Then i use this model in views.py like this:</p> <pre><code>def current_status(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/keenhome/accounts/login/') Current_Status = request.user.get_profile() on_off = Current_Status.turn_on_off auto_manu = Current_Status.auto_manu temp_dinningroom = Current_Status.temp_dinningroom temp_livingroom = Current_Status.temp_livingroom context = {'on_off': on_off, 'auto_manu': auto_manu, 'temp_dinningroom': temp_dinningroom, 'temp_livingroom': temp_livingroom, } return render(request, 'control/current_control_status.html', context) </code></pre> <p>The <code>on_off = Current_Status.turn_on_off</code> is where problem happens. ('UserProfile' object has no attribute 'auto_manu')</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload