Reputation: 561
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
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
to sync the model into database, it works well by calling UserProfile = request.user.get_profile()
, then i built Anther model class called "ControlInformation" in ANOTHER app (called "control"), using
def create_control_information(sender, instance, created, **kwargs):
if created:
ControlInformation.objects.create(user=instance)
post_save.connect(create_control_information, sender=User)
to sync this model, but there are some problems to use information in this model by calling Current_Status = request.user.get_profile()
, which is "'UserProfile' object has no attribute 'turn_on_off'" when calling on_off = Current_Status.turn_on_off
in views.py.
Am i right when building this one on one model in another app? Or is there any other problem?
Edit: my ControlInformation model is like this:
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)
Then i use this model in views.py like this:
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)
The on_off = Current_Status.turn_on_off
is where problem happens. ('UserProfile' object has no attribute 'auto_manu')
Upvotes: 0
Views: 138
Reputation: 99660
Since you already have request.user.get_profile()
to return UserProfile
model, You are looking for ControlInformation
object in this case. So, just change
Current_Status = request.user.get_profile()
to
Current_Status = ControlInformation.objects.get(user=request.user)
Or,
from django.shortcuts import get_object_or_404
Current_Status = get_object_or_404(ControlInformation, user=request.user)
Upvotes: 1