0

I use custom user model with two entities:

class WeddyUser(AbstractBaseUser):
    username = models.CharField(
    'Юзернейм',
    max_length=50,
    unique=True,
    db_index=True
    )
    email = models.EmailField(
    'Электронная почта',
    max_length=255,
    blank=True,
    null=True
   )
    is_vendor = models.BooleanField(
    'Волшебник',
    default=False
   )
   #other atributes and methodes

This class is inherited by two other user types:

class Vendor(WeddyUser):
   org_name = models.CharField('Название организации', max_length=50, blank=True)
   slug = models.SlugField('Короткое название', unique=True)
   city = models.ForeignKey(City, null=True, blank=True)
   description = models.TextField('О себе', blank=True)

and

class PlainUser(WeddyUser):
    date_of_birth = models.DateField('Дата рождения', null=True, blank=True)
    favor_news = models.ManyToManyField('news.News',  blank=True)
    favor_pic = models.ManyToManyField('news.Photo', blank=True)

I try to build personal area for this types of user:

in urls:

url(r'^desktop/$', views.DesktopView.as_view(), name='desktop'),

in template:

"{% url 'desktop'  %}" 

in view:

class LoginRequiredMixin(object):

@classmethod
def as_view(cls, **initkwargs):
    view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
    return login_required(view)


class DesktopView(LoginRequiredMixin,DetailView):
    model = WeddyUser
    context_object_name = 'weddyuser'

def get_context_data(self, **kwargs):
    context = super(DesktopView, self).get_context_data(**kwargs)
    if self.request.user.is_vendor:
        context['vendor']= Vendor.objects.get(id=self.request.user.id)
    context['plainuser']= PlainUser.objects.get(id=self.request.user.id)
    return context

def get(self, request, *args, **kwargs):
    c = {}
    c.update(csrf(request))
    user = request.user
    if user.is_vendor:
        return render_to_response('user/vendor_edit_profile.html', RequestContext(request, c))
    return render_to_response('user/user_edit_profile.html', RequestContext(request, c))

But, I can't get access to 'vendor' or 'plainuser' context objects, also 'weddyuser' context also doesn't work. I suppose my get function renders template without any context objects

M.javid
  • 6,387
  • 3
  • 41
  • 56

1 Answers1

0

change this code,

context['vendor']= Vendor.objects.get(id=self.request.user.id)
context['plainuser']= PlainUser.objects.get(id=self.request.user.id)

to this,

context['vendor']= Vendor.objects.get(weddyuser_ptr_id=self.request.user.id)
context['plainuser']= PlainUser.objects.get(weddyuser_ptr_id=self.request.user.id)

Pass context data in return statement,

return render_to_response('user/vendor_edit_profile.html', self.get_context_data() ,RequestContext(request, c))
return render_to_response('user/user_edit_profile.html', self.get_context_data(), RequestContext(request, c))
Geo Jacob
  • 5,909
  • 1
  • 36
  • 43
  • sorry, for what? If I add `abstract = True` I get `django.core.exceptions.ImproperlyConfigured: The model WeddyUser is abstract, so it cannot be registered with admin.` –  Jul 24 '15 at 08:13
  • I have editted the answer, please change the context['vendor'] and context['plainuser'] queryset. – Geo Jacob Jul 24 '15 at 08:50
  • I tried - nothing. As I said above, I think my get function doesn't pass any context object into template. Beacuse I can't use also context_object_name = 'weddyuser' –  Jul 24 '15 at 08:54
  • Don't make the WeddyUser Abstract, the way U did it, you will not have a duplicate ID between the Vendor and plain user table. If U do it as abstract, it is like defining two different tables with some common properties – Galia Ladiray Weiss Jul 24 '15 at 08:58
  • I've added self.get_context_data() and got the next error `AttributeError at /accounts/desktop/ 'DesktopView' object has no attribute 'object'` –  Jul 24 '15 at 09:13
  • Then I added `self.object = self.get_object()` and `self.get_context_data(object=self.object)` it works. But after I got `Generic detail view DesktopView must be called with either an object pk or a slug.` –  Jul 24 '15 at 09:16
  • It seems I found. I edited my url `url(r'^desktop/(?P\d+)/$', views.DesktopView.as_view(), name='desktop'),` and it's ok. Now I need only compare requested pk and pk of user instance –  Jul 24 '15 at 09:27