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.
icontainswould 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.icontainslookup, just useClient.objects.filter(user=current_user)andClientUpload.objects.filter(client=current_client)