1

I extended the User model in Django with a model called Client.

It looks like this:

@python_2_unicode_compatible
class Client(models.Model):
    user = models.OneToOneField(User)
    company = models.CharField(max_length=100)

    def __str__(self):
        return self.company

    class Meta:
        verbose_name_plural = _("Clients")
        verbose_name = _("Client")
        permissions = (
            ("can_upload", _("Can upload files.")),
            ("can_access_uploads", _("Can access upload dashboard.")),
            ("is_client", _("Is a client.")),
        )

However, I cannot figure out how to access this model through the current user in a view for example:

def dashboard(request):
    current_user = request.user
    current_client = Client.objects.filter(user__icontains=current_user)

    files = ClientUpload.objects.filter(client__icontains=current_client)

    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            new_file = UploadFile(file = request.FILES['file'])
            new_file.save()

        return HttpResponsePermanentRedirect('/dashboard/')
    else:
        form = UploadFileForm()

    data = {'form': form, 'client': current_client, 'files': files}
    return render_to_response('dashboard.html', data, context_instance=RequestContext(request))

ClientUpload Model:

@python_2_unicode_compatible
class ClientUpload(models.Model):

    client = models.OneToOneField(Client)

    def generate_filename(self, filename):
        url = "uploads/%s/%s" % (self.client.company, filename)
        return url

    file_upload = models.FileField(upload_to=generate_filename)

    def __str__(self):
        return self.client.company

    class Meta:
        verbose_name_plural = _("Client Uploads")
        verbose_name = _("Client Upload")

I get the current user with request.user but I can't figure out how to get the client related to that user. When I load the dashboard view I get the error:

"Related Field got invalid lookup: icontains"

How can I access the client model associated with a user when I extend the user model in this fashion according to the docs?

Thanks for any help or suggestions.

3
  • 1
    Why would you think icontains would be the right thing to do? That's what you use when you want to case-insensitively match part of a CharField against a string, which has nothing at all to do with what you're doing. Commented Jul 17, 2015 at 18:47
  • 1
    There is no need for the icontains lookup, just use Client.objects.filter(user=current_user) and ClientUpload.objects.filter(client=current_client) Commented Jul 17, 2015 at 18:51
  • Thanks that seems to work Commented Jul 17, 2015 at 18:56

1 Answer 1

3

You can access the Client object for a given User like this:

    client = request.user.client
Sign up to request clarification or add additional context in comments.

4 Comments

No need for the related_name. It is already client by default.
I tried accessing it that way and got the error:RelatedObjectDoesNotExist: User has no client.
Are you sure the User has a related client object?
It did work I was just logged in with the wrong account that didn't have Client associated\

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.