0

I'm trying to have a user profile page where users can manager the "customers" they are subscribed to. For testing purposes i'm calling the whole form {{ customer_subscription }} in the template, and it is returning the right instance of the user, but i'm expecting it to also have that user's subscribed "customers" highlighted on the form instance but it's not selecting anything. What am I doing wrong?

models.py

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    phone = models.CharField(max_length=10, blank=True)

    def __unicode__(self):
        return self.user.username

class Customers(models.Model):
    customer = models.CharField(max_length=200)

    def __unicode__(self):
        return self.customer

class Customer_Subscription(models.Model):
    user = models.ForeignKey(User)
    customers = models.ManyToManyField('Customers')
    def __unicode__(self):
        return (self.user)

forms.py

class CustomerSubscriptionForm(forms.ModelForm):
    class Meta:
        model = Customer_Subscription
        fields = '__all__'

views.py

def profile(request):

    userprofile = UserProfile.objects.get(user=request.user)
    customer_subscription = CustomerSubscriptionForm(instance=userprofile)
    return render_to_response('profile.html', {'userprofile' : userprofile, 'customer_subscription':customer_subscription, },context_instance=RequestContext(request))
1
  • You have created a model form for your Customer_Subscription model, so instance should be a Customer_Subscription instance. Instead, you are passing a user profile instead, which doesn't make sense. Commented Feb 2, 2014 at 1:06

1 Answer 1

1

When you create the ModelForm, have to use a CustomerSubscription object instance instead of a UserProfile

def profile(request):
    userprofile = UserProfile.objects.get(user=request.user)
    customer_subs = CustomerSubscription.objects.get(user=userprofile)
    customer_subscription = CustomerSubscriptionForm(instance=customer_subs)
    return render_to_response('profile.html', {'customer_subscription' : customer_subscription, 'customer_subscription':customer_subscription, },context_instance=RequestContext(request))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.