Skip to content
Advertisement

Tag: django

How do I force Django queryset return datetime field in current timezone?

I have Django model: When I get created_at value by queryset, it always return to me the datetime value with tzinfo=<UTC> like this: How do I force queryset return datetime value in current timezone (e.g Asia/Ho_Chi_Minh)? Answer Django always saves datetimes in UTC, and they are usually returned in UTC as well (depending on the database and database adapter settings).

Django Rest Framework JWT Unit Test

I am using DRF with the JWT package for authentication. Now, I’m trying to write a unit test that authenticates itself with a JWT token. No matter how I try it, I can’t get the test API client to authenticate itself via JWT. If I do the same with an API client (in my case, Postman), everything works. This is

How to generate indexes for related fields on Django models?

Say we’re building a Django-based site that clones Medium.com’s URL structure, where you have users and articles. We’d probably have this model: We want to be able to build URLs that look like /<username>/<slug>/. Since we’re going to have billions of articles and zillions of pageviews, we want to put an index on that model: But this causes the makemigrations

One view for multiple sub-domains using django-hosts

I need to have multiple sub domains for my project. Each sub domain represents some company. For example: company1.myproject.io, company2.myproject.io. I used django-hosts library to set up sub domains. hosts file: settings.py: core/hosts.py: hostsconf/urls.py: hostsconf/views.py: I have a few problems now: When I go to myproject.io it succesfully redirects me to the www.myproject.io. But when I go to company1.myproject.io I

Auto Fill User In Django Form

In my project as soon as user signup it is redirected to update view where he has to fill this information.Since the user has also logged in automatically after signup I want that user field to be filled automatically and can’t be edited. models.py forms.py views.py All the required libraries are imported. Answer You can use widgets for your form.

django ‘str’ object has no attribute ‘_meta’

Sorry for my English. I have some data from another server, but I need to output this data like JSON. if i print response in console: but, if i return this response like HttpResponse i have an error AttributeError: ‘str’ object has no attribute ‘_meta’ this my code: UPD: I tried with this: but have error: Object of type ‘ModelYears’

Django: Gmail SMTP error: please run connect() first

I am trying to send mail when a certain query is executed. But I am getting error in the connection. I have tried the following settings in my settings.py file I have executed the following command to send the email: But whenever I run the above code I get ‘please run connect() first’ error. What is the error Exactly about

Prevent DateRangeField overlap in Django model?

Now that Django supports the DateRangeField, is there a ‘Pythonic’ way to prevent records from having overlapping date ranges? Hypothetical use case One hypothetical use case would be a booking system, where you don’t want people to book the same resource at the same time. Hypothetical example code Answer I know that the answer is old, but now you can

Advertisement