Filter query using headers in DRF - django

I have a use case where I need to show the data of the company the user belongs to. I don't want the url to show something like: 127.0.0.1:8000/api/document?company=somecompany rather I want to pass the company within the header and return the data related to the company.
Is there any way to achieve this in Django REST Framework? Else, how can I avoid 127.0.0.1:8000/api/document?company=somecompany.

You can use request.META.get('NameOfYourHeader') and set the custom header from the frontend.
Now be aware that this will real bad practise and query params are meant for this. Also if you want to filter on some user, there may be some workarounds like request.user or nested serializer from a user instance.

Related

In a Django Rest Framework view, does request.user make a database call or the database call happens before the request reaches the view?

I need to retrieve some information about my users and I am trying to avoid making unnecessary database calls.
The information I need is stored in three models: User, UserProfile and Membership.
Both UserProfile and Membership have a OneToOne relationship with the User model.
I know that I can use select_related() to retrieve related models from the database in a single call. So I could do something like:
User.objects.select_related('userprofile').select_related('membership').get(id=request.user.id)
But that must not be correct, because if I am using some user information to do the query, it means I already retrieved this information in a previous call.
So what would be the best way to get this information minimising database calls? Would it be even possible to get the information from these 3 models in a single call?
DRF performs user related DB query inside authentication class. See source here. So if you need to optimize this query you should implement custom autentication class(see details here), override authenticate_credentials method and use optimized query inside it.

How to use model-form in flatpages django?

I stuck in code where i want to use model-form in flatpage template.
I create model-form and that form need to serve in flatpage template with save functionality.
(I know this is old, but just in case other are looking at it)
I don't think you can as flat-pages are associated 'simple “flat” HTML content'. This include stuff like 'About-us' pages or private policies.
Usually you want to maintain Forms data within Views which you can access their data from it. This allows you to use the data to send emails or process logins.

Managing UserContext in Django

Im using the following statement to set Language Option in my projects which works as expected.
request.session['django_language'] = "de"
This is fine with in View, but when the control goes to other files to connect to DB or external services how can I access it. I dont want to pass the request object through out all the application.
If something like UserContext/RequestContext where every request has to go-through it (Middleware) so that I can set it there and access it without help if request object.
I understand from the headline that you want to store the language per user.
It might be best to extend the user model and add a model field for preferred_language.

Django admin particular data for particular user

I am using django admin site to let people manage database easier.
For some reason, I want to hide some data from some user.
Let's say I have a model named Book and there are a lot of books in database. I want different user has the different scope of books he can view.
How would I do that?
I am thinking about permission. Is that possible to set the permission to filter the data?
I know how to create permission according to a specified model. However, after that, how do I suppose to use that permission? I believe I may need to override part of "changelist_view" method under BookAdmin class, right?
Any help would works.
Thanks in advance
Use the queryset method on your admin model. Something like:
class BookAdmin(admin.ModelAdmin):
def queryset(self, request):
return super(BookAdmin, self).queryset(request).filter(owner=request.user)
Obviously the filter will vary depending on your book model, but this is the general idea.

Django databrowse with custom queryset?

Django's databrowse is very different from the rest of django in that the docs literally don't exist. Has anyone tried to do more that databrowse.site.register on a model? Any code examples?
In particular, I've got a model that has a ForeignKey to an auth.Group and I want databrowse to use this queryset instead of .all():
qs = Model.objects.filter(group__in=request.user.groups.all())
Bonus points for making it possible to have a button that does stuff with the current object (edit/delete/clone/etc). I basically need a simple way to browse and edit rows without giving users access to the admin.
It'd be even better if there was a way to do that on the admin, but I don't want to give users the staff privilege.
There's no way to do this through databrowse. You could try writing a custom Manager for your Model and return the required query set by default.