Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to use django rest api to create user and how to define user level?
Create REST APIs based on Django with PostgreSQL database. It should contain: User Sign Up/Forgot Password APIs. Uses JWT authentication. Must define 3 user levels: 1. Super-admin, 2. Teacher, 3. Student (Use internal Django Groups to achieve the same). Teacher must be able to add/list the students. Admin must be able to add/list every user in the database. Students must be able to see his information only I have to solve the above assignment myself but didn't understand the question from 3rd instructions. I know to create groups and users from the Django admin panel but I think they expect to create users using API. What I am thinking to do is to create groups of super-admin, student, and Teacher manually from the admin panel. import User from from django.contrib.auth.models import User and save data to it.(Here the problem is I don't know how to import serializer for User or I should create it) And then add user to group (student, Teacher or super-admin) Please help me to understand 3rd instruction. -
Solution to installing GDAL/PROJ/GEOS in windows 10 for Django/Geodjango
Whether you are finding it difficult to install the GDAL library or have been able to install it but do not know how to make it work in Django for your geoDjango app, I would like to share how I was able to successfully get it to work for me. I hope I would not miss any step as it took me days to make it work. -
Django Channels cannot establish Websocket connection with Custom middleware
I'm learning Django Channels and trying to make my custom Authentication middleware. I start with the code from the official docs. I try to set up a connection via Postman but it doesn't work. It works well if I remove the middleware in my asgi.py Here my code: # routing.py websocket_urlpatterns = [ re_path('ws/chat/hihi', ChatRoomConsumer.as_asgi()), ] # asgi.py application = ProtocolTypeRouter({ 'http': get_asgi_application(), 'websocket': QueryAuthMiddleware(URLRouter( chatroom.routing.websocket_urlpatterns )) }) My middleware is copied from the official docs. @database_sync_to_async def get_user(user_id): try: return User.objects.get(id=user_id) except User.DoesNotExist: return AnonymousUser() class QueryAuthMiddleware: """ Custom middleware (insecure) that takes user IDs from the query string. """ def __init__(self, app): # Store the ASGI application we were passed self.app = app async def __call__(self, scope, receive, send): # Look up user from query string (you should also do things like # checking if it is a valid user ID, or if scope["user"] is already # populated). scope['user'] = await get_user(int(scope["query_string"])) return await self.app(scope, receive, send) Here is my simple consumer class ChatRoomConsumer(JsonWebsocketConsumer): def connect(self): print('connect....') if self.scope['user'].is_anonymous: self.close() self.accept() def disconnect(self, code): print('ahihihihihi') raise StopConsumer def receive(self, text_data=None, bytes_data=None, **kwargs): text_data_json = json.loads(text_data) message = text_data_json['message'] self.send(text_data=json.dumps({ 'message': message })) With this, I cannot establish connection … -
Best way to include a sub-select queryset in Django in just 1 hit to the database
I have 2 models, Product and Productdetails with a OneToOne relationship like this: class Product(IotaModel): stripe_product_id = models.CharField( blank=True, max_length=100, help_text='Product ID in Stripe.' ) details = models.OneToOneField( ProductDetails, null=True, on_delete=models.SET_NULL ) I want a queryset that do this: SELECT * FROM product_details WHERE id = ( SELECT details_id FROM products WHERE id=<product_id> ) I tried this: details_id = Product.objects.filter(pk=product_pk, active=True).only('details')[:1] return ProductDetails.objects.filter(pk=details_id, active=True) But it does not work becouse .only('details') gave me the fields (id, details_id) and the next filter takes id instead of details_id. I also try to add .defer('id') but that does not work. I know that I can get the details_id using .values_list('details_id') but I think this would involve making 2 queries to the Database. What could be the best approach to reach the desired SQL query in just 1 hit to the DB? Thanks in advance! -
Gitlab CI/CD Pipeline Runs Django Django Runs Unit Tests Before Migrations
Problem I'm trying to set up a test stage in Gitlab's CI/CD. Locally, running the unit tests goes fine and as expected. In Gitlab's CI/CD, though, when running the script coverage run manage.py test -v 2 && coverage report the unit tests are executing before the migrations are completed in the test database, which is unexpected, and will always fail. Migrations on the test database need to run prior to unit tests executing. Any idea why this behavior might happen? Things I've already tried Removing coverage, and only executing python manage.py test Result: same error Removing user test, where Gitlab says the test fails: Result: same error Removing all but one unit test to attempt to isolate: Result: same error Adding and activating a virtual environment inside Gitlab's container: Result: same error Trying a SQLite3: Result: migrations fail due to necessary array field necessary in the project not supported in SQLite3 Gitlab output My actual output is much bigger. But here's the relevant parts: $ echo $TEST_SECRETS | base64 -d > config/settings/local.py $ echo RUNNING UNIT TESTS RUNNING UNIT TESTS $ coverage run manage.py test -v 2 && coverage report Creating test database for alias 'default' ('test_mia')... test_dataconnector_category_creation_delete (apps.core.tests.test_models.DataConnectorCategoryTestCase) Verify … -
Django Rest Framework Serialize Model into Object with Custom Key
I am working with Django and djangorestframework. What should I modify to get desired output? serializers.py: from rest_framework import serializers from django.contrib.auth.models import User class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ['id', 'username'] views.py: from django.contrib.auth.models import User from rest_framework import viewsets from .serializers import UserSerializer class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer current output: [ { "id": 1, "username": "some_username" }, { "id": 2, "username": "another_username" } ] desired output: { "some_username": { "id": 1 }, "another_username": { "id": 2 } } -
Url Redirection in django
I have an app created in django and deployed at KeshavBits heroku. And after deploying app I purchased a domain (https://keshavbits.in/) and managed to point it to https://keshavbits.herokuapp.com/ So now I have two domains to acccess my website https://keshavbits.herokuapp.com/ https://keshavbits.in/ Both links shows the same website i.e hosted at heroku but the problem is when someone logs in at keshavbits-heroku then session is stored under its domain so when they visit keshavbits.in they have to log in again. I have checked django documentation and other stuff as well about multiple session manager but it works only when website is like home.keshavbits.in and play.keshavbits.in So I thought to redirect traffic to keshavbits.in when someone hits https://keshavbits.herokuapp.com/ But I don't have any idea to do that. Kindly help me :) -
Django user based access control to uploaded files to s3 bucket
I'm working on an django application where users can upload large files (>3GB) to s3 bucket. How can I implement an user based access control for these uploaded user files. Only the user who uploaded the files and specific role should have access to the files. -
Django filters without submit button and with HREF
I am looking for a way to create a filter on my Django page very similar to what's on the Amazon website. For example the 'BRANDS' filter for laptops have the HREFs predefined so you can middle click on that and after filtering (which can also be multiple selections) the selections stay activated and clicking again on something deactivates it. Each time you click on a selection it goes straight away to a new link so there's no submit button involved. After doing some searching I found this solution: https://stackoverflow.com/a/60857158/18784545 Just wondering is there a more elegant / built-in way to achieve this? As the comment says, that solution is pretty verbose. -
ImportError: cannot import name 'force_str' from partially initialized module 'django.utils.encoding' (most likely due to a circular import)
I went to my utils.py and added and replaced their corresponding fields this: from django.utils.encoding import force_str plus this def _camelize_django_str(s): if isinstance(s, Promise): s = force_str(s) return to_camel_case(s) if isinstance(s, six.string_types) else s But I still get the same error, what can I do? -
Django rest how to do pagination with PageNumberPagination
I wrote the following codes. But when I go to the url posts?page=1, it still shows all of the Post model objects. What should I do? settings.py REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', 'PAGE_SIZE': 10 } ursl.py path('posts', views.posts_view, name='posts_view') views.py @api_view(['GET']) def posts_view(request): posts = Post.objects.all() serializer = PostSerializer(posts, many=True) return Response(serializer.data) -
Django allauth return Account Inactive page after login
I am new to programming and I don't fully understand how allauth work and what exactly to do. I have an application where the user is inactive after signing up and he must click on the confirmation email so that he becomes active. I tried to configure allauth so that a user can also log in with google, but when a new user logs in he is redirected to a page that says Account Inactive.In admin I can see that it creates an account (inactive) and also an entry in social accounts but it doesn't generate a social application token. On the other hand when a user that already has an acount tries to log in with google it redirect to allauth sign up page. And so I don't understand how activation with allauth works. Did I make something wrong with my allauth configuration? Should I edit my login function or something else? -
upload a file and get celery progress bar using ajax in django
I want to upload a file, process it in a Celery task, and show a progress bar using AJAX. but I did not get the solution. Can you help me with this task? Views.py def index(request): cntx = {} if request.method == 'POST': form = RawFileAddressForm(request.POST, request.FILES) if form.is_valid(): file = request.FILES['file'] fs = FileSystemStorage() file_name = fs.save(file.name, file) task = process_file.delay(file_name=file.name) cntx['task_id'] = task.id # return render(request, 'index.html', cntx) return HttpResponseRedirect(reverse('get_task_info') + '?task=' +task.id) else: return render(request, 'index.html', {'form':form}) else: form = RawFileAddressForm() return render(request, 'index.html', {'form': form}) get_task_info file def get_task_info(request): if 'task' in request.GET: task_id = request.GET['task'] else: return HttpResponse('no job id given') task = AsyncResult(task_id) data = { 'state': task.state, 'result': task.result, } return HttpResponse(json.dumps(data), content_type='application/json') forms.py class RawFileAddressForm(forms.Form): file = forms.FileField() Task.py @shared_task(bind=True) def process_file(path, file_name): print('Uploading image...') sleep(10) fs = FileSystemStorage() instance = Tooltype(image=file_name) instance.save() fs.delete(file_name) print('Uploaded!') index.html <body> <h1>select your file to process it!</h1> <progress id="progress-bar" value="0" max="100" style="display:none; margin-bottom: 1em;"></progress> <form id="process-raw-data" action="" method="post" enctype="multipart/form-data"> {% csrf_token %} {{ form }} <input type="submit" value="Submit" /> </form> {% if task_id %} <script type="text/javascript"> var taskid = "{{task_id}}"; var frm = $('#process-raw-data'); var pgrbar = $('#progress-bar'); get_task_info(taskid); function get_task_info(tid) { $.ajax({ type: 'get', url: … -
How to design to avoid duplicate column in two models that have the same ForeignKey?
I have the following models where a User has many Photo objects and many Album objects. However, after a User has added a given photo they may choose to put it in a single Album (a Photo does not have to have an album linked to it). They may also choose to take it out of the album and or put it in a different album. class User(models.Model): id = ... class Photo(models.Model): id = ... user = models.ForeignKey(User, ...) album = models.ForeignKey(Album, null=True...) class Album(models.Model): id = ... user = models.ForeignKey(User, ...) How do I get around the redundant user column (Album.user and Photo.user) when a Photo is associated with an Album to avoid db anomalies? I can't get rid of the user column on Photo since I wouldn't be able to get it if it is not linked to an album. The same goes for Album. I have some experience with normalizing tables but I don't know how to avoid this without using db constraints. -
Django: relation between permissions and group
Why this works: g = Group.objects.get(pk=1) p = g.permissions.first() Out[43]: <Permission: onboarding | controller | Can add controller> But this doesn't work: p.group AttributeError: 'Permission' object has no attribute 'group' When I do: p._meta.__dict__ I see: 'fields_map': {'Group_permissions+': <ManyToOneRel: auth.group_permissions>, 'group': <ManyToManyRel: auth.group>, 'User_user_permissions+': <ManyToOneRel: core.user_user_permissions>, 'user': <ManyToManyRel: core.user>} So my question is, why can't I do p.group ? -
Django ORM annotation to find how many objects have any related object
I am currently trying to find out what % of a set of objects have a related object with certain values. Specifically, I have a table of objects and a one to many relationship to a table of comments, and I am trying to figure out what percentage of those objects have comments in a specific length. Both of these tables are ETL output from a separate dataset to allow easier metric calculations. # Models class Data(models.Model): id = models.AutoField(primary_key=True) creator_id = models.IntegerField() # Not a real foreign key class DataCommenter(models.Model): do_id = models.ForeignKey(Data) creator_id = models.IntegerField() # Not a real foreign key short_comments = models.IntegerField() medium_comments = models.IntegerField() long_comments = models.IntegerField() From these models, I have some queryset annotations that are being performed to try and get the average as shown below: # QuerySet class DataQuerySet(models.QuerySet): def extensive_comments(self): """Get extensive comment raw selection.""" inner_query = DataCommenter.objects.exclude( creator_id=OuterRef("creator_id") ).filter( Q(medium_comments__gte=1) | Q(long_comments__gte=1), do_id=OuterRef("id") ) return self.annotate( raw_extensive_comments=Case( When( Exists(inner_query), then=1 ), default=0, output_field=FloatField() ) ) def annotate_period(self): """Annotation to allow average without aggregation.""" return self.annotate(period=Value(value=True, output_field=BooleanField())) The QuerySet is attached to the Data model and is used as follows: Data.objects.all().annotate_period().extensive_comments().values("period").annotate( extensive_comments=ExpressionWrapper(Avg(raw_extensive_comments) * 100, output_field=FloatField()) ) The data that we have … -
django chunk query set
i need to chunk my data for 8 items in every tr in my HTML file how can i do that in Django {% for pr in portfolio %} <td> <img src="{{ pr.logo.url }}" class="img-responsive" title="{{ pr.title }}"> </td> {% if forloop.counter %} </tr> <tr> {% endif %} {% endfor %} -
Multiple tables operation causes dead lock
When I was doing performance tunning on an api backended by django. I found it was easy to raise an error like this: dead lock found... Code causing deadlock(I've simplicified for clarity): @require_POST def create_sop_task(request): try: with transaction.atomic(): save_id = transaction.savepoint() db_tpl_obj = SopTemplate.objects.filter(id=template_id).values("type", "status").first() db_new_st = SopTask.objects.create(**req_args) for i, item in enumerate(message): SopMessage.objects.create(**kwargs) SopTemplate.objects.filter(id=template_id).update(version_time=version_time) SopTask.objects.filter(template_id=template_id).update(version_time=version_time) SopMessage.objects.filter(template_id=template_id).update(version_time=version_time) transaction.savepoint_commit(save_id) return JsonResponse({"code": 0, "msg": "success", "data": {"id": db_new_st.id}}) except Exception as e: # ... Manipulating order in transaction: create SopTask create SopMessage update SopTemplate update SopTask update SopMessage If tweak the order like this, everything goes well: create SopTask update SopTask create SopMessage update SopMessage update SopTemplate But I can not figure it why this will cause dead lock. Someone could explain that? I'll appreciate a lot. I am using MySQL 5.7 with isolution level: READ COMMITTED. -
How to create a django form field with html
at the moment the html page looks something like this: <form action="{% url 'sequence_details' %}" method="post"> {% csrf_token %} <div class="container pb-3"> <table class="table"> {{ form.as_table }} <tr><th><input type="text" id="extrafield"></th></tr> </table> </div> <div class="container"> <input type="submit" class="btn btn-primary" value="Download"> </div> </form> extrafield appears as a text field but any text entered into it does not get sent to the backend, i.e. the result of form.cleaned_data.get("extrafield") is always None, no matter what was entered. I would like to be able to customise extrafield and write my own html for it, which is why I'm not just adding it to the form object. How can I get the result of extrafield's input to show up in form.cleaned_data? -
Insert a foreign key in my database using django
I want to insert a foreign key from a campaign table in a task table.I want get this foreign key from the url. Here is my models : Campaign Model : class Campaign(models.Model): title = models.CharField(max_length=255) channel = models.CharField(max_length=255) start_date = models.DateField() end_date = models.DateField() Task Model : class Task(models.Model): title = models.CharField(max_length=255) description = models.CharField(max_length=255) task_manager = models.CharField(max_length=255) start_date = models.DateField() end_date = models.DateField() resource = models.ManyToManyField(Resource,related_name='resources') campaign = models.ForeignKey(Campaign,on_delete=models.CASCADE) And here is my view : def campaign_tasks(request,pk): tasks = Task.objects.all() managers = TaskManager.objects.all() context = {"tasks": tasks,"managers":managers} if request.method == 'POST': title = request.POST['title'] description = request.POST['description'] task_manager = request.POST['task_manager'] start_date = request.POST['start_date'] end_date = request.POST['end_date'] campaign_id = request.GET['pk'] Task.objects.create(title=title,description=description,task_manager=task_manager,start_date=start_date,end_date=end_date,campaign_id=campaign_id) return render(request,'CampaignManagement/campaign_tasks.html',context) -
How handle pip and pip cache in windows?
I'm stuck in pip installation process.. 1.how can ignore pip cache 2.how can remove all packages for pip 3.can pip/cache folder deletion is safe -
TypeError at /MultipleChoiceQuestions/ MultipleChoiceQuestions() missing 1 required positional argument: 'id'
def MultipleChoiceQuestions(request,id): # return HttpResponse("Hii Here loveQutes are available") post = MCQ.objects.filter() print(post) print(post[0]) question = MCQ.objects.get(pk=id) output = ', '.join([ print(q) for q in question]) # return render(request, "blog/blogHome.html", context) return render(request, "index.html",{'output': output} ) -
Special characters change when exporting django
I'm using django import export package and MySQL for the database. Everything works fine, except for one thing. When I exported, the data that has "special characters" changed. For example Château will be changed to Château. Even though, everything is displayed correctly in the admin. How can I fix this? Any solutions? Thanks. -
Get timezone by Country and State/Province in Python
Is there a way to retrieve timezone information based on Country and State/Province in Python? E.g. The United States and New York will get EST (Eastern Standard Time). If that's not possible or efficient, is there a way to get timezone based on Country and City instead? I'm using Python Django in my project. Thanks in advance. -
Cannot check if user is in specific queryset in template
I'm working on an app similar to twitter. I have pages with detail tweet and it's ancestors and descendants, and in template i want to check if current user have liked tweets which are displayed on the page. My tweet model is: class Tweet(MPTTModel): text = models.CharField(max_length=140, db_index=True) pub_date = models.DateTimeField(auto_now_add=True) likes = models.ManyToManyField(User, blank=True, related_name='liked_tweets') retweets = models.ManyToManyField(User, blank=True, related_name='retweeted_tweets') user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tweets') parent = TreeForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, related_name='children') In template i use if clause: {% if user in tweet.likes.all %} # do smth here {% else %} # do smth else {% endif %} Although i have access to user instanse in template with {{ user }}, if clause always returns false even if user in queryset.