Is it possible to conditionally register or unregister models in django admin? I want some models to appear in django admin, only if request satisfies some conditions. In my specific case I only need to check if the logged in user belongs to a particular group, and not show the model if the user (even if supe…
Tag: django
How to show query parameter options in Django REST Framework – Swagger
This has been bugging me for a while now. My ultimate goal is to show query parameter options inside SwaggerUI and give a form input for each query parameter. Similar to how it is displayed when providing a serializer for POST. I am using a viewset which inherits from GenericViewSet and I have tried the follo…
Get current user in Model Serializer
Is it possible to get the current user in a model serializer? I’d like to do so without having to branch away from generics, as it’s an otherwise simple task that must be done. My model: My serializer: And my view: How can I get the model returned, with an additional field user such that my respon…
Django Management Command Argument
I need to pass in an integer argument to a base command in Django. For instance, if my code is: I want to run: so, it will return 16. Is there a way I can pass an argument through the terminal to the command I want to run? Answer Add this method to your Command class: You can then use
Django Model MultipleChoice
I know there isn’t MultipleChoiceField for a Model, you can only use it on Forms. Today I face an issue when analyzing a new project related with Multiple Choices. I would like to have a field like a CharField with choices with the option of multiple choice. I solved this issue other times by creating a…
How to run Debug server for Django project in PyCharm Community Edition?
Has anyone had issues setting up a debug configuration for Django project in PyCharm Community Edition? Community Edition of the IDE is lacking the project type option on project setup and then when I am setting up Debug or Run config it asks me for a script it should run. What script would it be for Django, …
What is the simplest way to retry a Django view upon an exception being raised?
I want to rerun a Django view function if a certain exception is raised (in my case a serialization error in the underlying database). I want it to run with exactly the same parameters, including the same request object – as if the client had re-requested the URL. There are many database queries in the …
Dynamically creating Django models with `type`
I have 20+ MySQL tables, prm_a, prm_b, … with the same basic structure but different names, and I’d like to associate them with Django model classes without writing each one by hand. So, feeling ambitious, I thought I’d try my hand at using type() as a class-factory: The following works: But…
Django authenticate using logged in windows domain user
I want to authenticate django web user using windows domain account (active directory) who currently logged in to computer. How can I do this without prompting user to enter username/password again since he is already logged in using domain account to his system. I am using django and python 2.7. I went throu…
Javascript to Django views.py?
This may sound simple, but how do I send the data from a Javascript array in my index.html template to my views.py? When the user clicks a “Recommend” button, my code calls a function that accesses my database and prints a name on the template. I have an array of checkbox values in Javascript that…