Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django slow execution of python code (no ORM)
I am seeing massively degraded DRF performance for simple python code when running under an API vs. standalone. For e.g., running the python code standalone. I am loading a user file, converting it into a custom object and running some validations on it. xlsname = 'user_file.xlsx' dash = Dashboard(xlsname,logging.ERROR,create_copy=True) #loading user file Load time: 1.828125 dash.validate() #validate user file Validation time: 0.203125 Now when running the same code under a simple DRF function view: #views.py @api_view(['GET']) def load_and_validate(request): xlsname = 'user_file.xlsx' dash = Dashboard(xlsname, logging.DEBUG, create_copy=True) dash.validate() return Response({"message": "{} loaded".format(xlsname)}) My response times are: Load time: 1.96875 #this is in line with standalone execution **Validation time: 5.21875 # this is 2500x slower than standalone!** The validation time for my file is now 2500x slower than standalone! I am not even using any ORM yet so there are no queries. Everything is loaded into the memory and executed from there. What may be causing this and how can I diagnose it? -
Postgresql ArrayField vs ForeignKey? which one is performant?
I want to design a phone book application that each contact can have multiple numbers. There is two db designs: use contact foreign key in each number. store numbers in an ArrayField inside each contact. which solution is more performant in production and why? Thanks in advance. -
URL blocked: This redirect failed because the redirect URI is not white-listed in
I try to login with facebook using django but it shows that error.. enter image description here -
Where is the best course of Python with Django for free?
Can u send me someone good course to learn python with Django? I have good knowledge with Python, but Django is a bit hard, can help me?? -
How to use update_or_create with defaults argument
I have the model League class League(models.Model): league = models.IntegerField(primary_key=True) league_name = models.CharField(max_length=200) country_code = models.ForeignKey("Country",null=True, on_delete=models.SET_NULL) season = models.ForeignKey("Season", null=True,on_delete = models.SET_NULL, to_field = "season") season_start = models.DateField(null = True) season_end = models.DateField(null = True) league_logo = models.URLField(null = True) league_flag = models.URLField(null = True) standings = models.IntegerField(null=True) is_current = models.IntegerField(null=True) I created objects from this model. After it i needed to add some additional fields to League model after adding those fields League object became so class League(models.Model): league = models.IntegerField(primary_key=True) league_name = models.CharField(max_length=200) country_code = models.ForeignKey("Country",null=True, on_delete=models.SET_NULL) season = models.ForeignKey("Season", null=True,on_delete = models.SET_NULL, to_field = "season") season_start = models.DateField(null = True) season_end = models.DateField(null = True) league_logo = models.URLField(null = True) league_flag = models.URLField(null = True) standings = models.IntegerField(null=True) is_current = models.IntegerField(null=True) cover_standings = models.BooleanField(null=True) cover_fixtures_events = models.BooleanField(null=True) cover_fixtures_lineups = models.BooleanField(null=True) cover_fixtures_statistics = models.BooleanField(null=True) cover_fixtures_players_statistics = models.BooleanField(null=True) cover_players = models.BooleanField(null=True) cover_topScorers = models.BooleanField(null=True) cover_predictions = models.BooleanField(null=True) cover_odds = models.BooleanField(null=True) lastModified = models.DateTimeField(auto_now=True) I did migrations and added these fields to db schema. Now i want to add to these added fields values. I read about update_or_create method and tried to use it for updating League model objects leagues_json = json.load(leagues_all) data_json = leagues_json["api"]["leagues"] for item in data_json: … -
How do you allow input to be an object selection from model but also free text input in a django form?
I am trying to have a field in a form that allows users to select objects from a Model and also allows free text input. I am using django-autocomplete-light. Although it works great for selecting choices from the model, I can't get it to allow free text input without creating new objects before the form is submitted. Before someone marks this as duplicate, I did read this question but it did not guide me into a solution. Here's my models.py: class Person(models.Model): name = models.CharField(max_length=255, blank=True, null=True) My forms.py: class NameForm(forms.ModelForm): name = forms.ModelChoiceField( queryset=Person.objects.all(), widget=autocomplete.ModelSelect2(url='myapp:name-autocomplete') ) class Meta: model = Person fields = ['name',] My views.py: class PersonAutoComplete(autocomplete.Select2QuerySetView): def get_queryset(self): qs = Person.objects.all() if self.q: qs = qs.filter(name__icontains=self.q) return qs And my urls.py: path( 'name-autocomplete/', PersonAutoComplete.as_view(create_field='name'), name='name-autocomplete', ), -
post Blocks across views
I stuck on something in django 2.2 that I can not find an explanation on the forums or in the tutorial, is that I want to display the blocks of a basic template through the views in a single template, kind for validated a form the page does not refresh, and display another block content that will be displayed with another view, I hope my question was clear and thank you for your answers. -
How to write data to MongoDB using PyMongo as well as Django
I am new to Django and MongoDB. I used Djongo to connect to MongoDB from Django. I also updated models.py based on the model I wish to implement. Now, I need two ways to write/update data to MongoDB for my application. From front-end of the website using Django forms. From back-end using PyMongo. I was able to successfully execute the below two commands. python manage.py makemigrations python manage.py migrate This created the required collection in mongodb. The problem is when I push data from backend using PyMongo, I'm able to insert one document to this collection. But unable to add anymore document to this collection. It is giving me the below error. pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: FLASH.home_street index: __primary_key__ dup key: { : null } PyMongo is expected to generate unique _id for each document. I also don't understand where this __primary_key__ came from. I didn't define this key in my model though. Kindly help. -
Django user management best practices
I'm building an API for a mobile app using django and I'm wondering what are the best practices for managing the users of my app? Should I extend the default User model provided by django (which provides fields for admins as well) or should I create my own User model and figure out authentication for it? The default User model has things more related to admins like permissions and groups. Is this model meant to be used by non-admin users as well? -
How can get image which is uploaded in HTML page , In Django
I made an HTML form which uploaded the image and that image I have to use in Django Views to perform something of that image, how can I do? -
Django + uWSGI server stuck when accessing Twitter
I'm operating the Django + uWSGI application with social-auth-app-django package. (social-auth-app-django: https://python-social-auth-docs.readthedocs.io/en/latest/configuration/django.html) My problem is that the uWSGI server sometimes stuck, or all workers are busy. I was getting to the root of the problem for months, and then I found that a uWSGI process stuck for a long time (60 sec+) after dumping the following message (I used strace command): socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_TCP) = 14 connect(14, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("104.244.42.194")}, 16 "104.244.42.XXX" is Twitter API server's IP. I'm wondering, however, why it takes for a long time. I guess that this request is OAuth because my application uses only Twitter OAuth API, so I set a timeout to the configuration file of social-auth-app-django, as follows: SOCIAL_AUTH_URLOPEN_TIMEOUT = 5 but it never resolve the probrem... I want to know: Is there a case Twitter OAuth takes such a long time, and Why? Is it really OAuth request, or something else? -
in django models.py how to make remain quantity column of item table change automatically?
class Item(models.Model): name=models.CharField(max_length=100) price=models.DecimalField(max_digits=6, decimal_places=2) quantity=models.IntegerField(default=0) sold=models.IntegerField(default=0) qtt=0 def countremain(self): qtt=self.quantity-self.sold return qqt remain=models.IntegerField(qqt) in admin page I can change manually remain value, but I want to make it automatically. what I have to change or add to the code above? Please help. Thanks in advance! -
Use test db for Django's built-in testing with SQLAlchemy ORM
I have a project running in Django and connecting do SQLAlchemy ORM via sessionmaker, as showed below. It basicly handles http methods with a specified API (GET, POST, DELETE) and returns, posts, updates or deletes db entries. from sqlalchemy import create_engine from sqlalchemy.orm.session import sessionmaker self.session = sessionmaker( bind=create_engine('mysql+pymysql://user:pw@127.0.0.1/db') Under myproject.settings I am using defaults like 'ENGINE': 'django.db.backends.sqlite3',. I would like to test whether the API is working as intended by simply interating through all possible methods and URIs which seem necessary to test. Testing is done with Django's TestCase class and its Client module. Works quite fine. My Problem: It is altering (especially deleting and updating columns within) the real db. Rather than using the "created and destroyed test_db" as Django's test output might indicate it is using the real db. I kinda get why (I am bypassing Django's built-in db-connection with my SQLAlchemy connection), but I am interested in how to fix this, i.e. using a true test_db. Currently I am using a read-only mysql-user for testing, but that prevents me from testing actual POST and DELETE requests. I could try to use a different db for testing by mocking, but I would prefer another solution (I would … -
Django 1.11 - How can I ensure TruncYear to produce Zulu time
I am using Django 1.11 and Postgres 9.4. How can I ensure TruncYear to produce Zulu time (2019-10-01T00:00:00Z). I notice it creates datetime with timezone like this (2017-01-01T00:00:00+03:00) Here is my code for the TruncYear queryset: from django.db.models import Count from django.db.models.functions import TruncMonth, TruncYear, TruncDay, TruncHour tracking_in_timeseries_data = Tracking.objects.annotate( year=TruncYear('created_at')).values('year', 'venue').annotate( count=Count('employee_id', distinct = True)).order_by('year') >>> for exp in tracking_in_timeseries_data: ... print(exp['year'], exp['venue'], exp['count']) 2017-01-01 00:00:00+00:00 4 1 2019-01-01 00:00:00+00:00 2 2 2019-01-01 00:00:00+00:00 3 1 2019-01-01 00:00:00+00:00 4 1 2019-01-01 00:00:00+00:00 5 1 2019-01-01 00:00:00+00:00 6 1 >>> tracking_in_timeseries_data <QuerySet [{'venue': 4, 'year': datetime.datetime(2017, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}, {'venue': 2, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 2}, {'venue': 3, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}, {'venue': 4, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}, {'venue': 5, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}, {'venue': 6, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}]> And if I serialize it produce this: serializer.py class TimeseriesYearSerializer(serializers.ModelSerializer): venue = VenueTSSerializer(read_only=True) year = serializers.DateTimeField(read_only=True) count = serializers.IntegerField(read_only=True) class Meta: model = Tracking fields = ['venue', 'year', 'count'] output: [ { "count": 1, "year": "2017-01-01T00:00:00+03:00", "venue_id": 2 }, { "count": 1, … -
Django Rest Framework: serializer for two models that inherit from a common base abstract model
There is an abstract model that defines an interface for two child models. I've been asked to create an API endpoint that will return instances from those child models (including only the common fields defined thanks to the interface father class). The problem raises when defining the Serializer.Meta.model attribute. Anyway, code is always clearer: models.py class Children(Model): class Meta: abstract = True def get_foo(self): raise NotImplementedError class Daughter(Father): def get_foo(self): return self.xx class Son(Father): def get_foo(self): return self.yy api/views.py class ChildrenApiView(ListAPIView): serializer_class = ChildrenSerializer def get_queryset(self): daughters = Daughter.objects.all() sons = Son.objects.all() return list(daughters) + list(sons) serializers.py class ChildrenSerializer(ModelSerializer): foo = CharField(source="get_foo", read_only=True) class Meta: model = Children # <========= HERE IS THE PROBLEM fields = ('foo',) Some thoughts; I know I'm not able to point out to the abstract model Children (wrote it for showing the inntention) I tried to leave ChildrenSerializer.Meta.model empty Seems that I can choose whichever Daughter or Son but not sure if that solution has any side-effect or is the way to go. -
Django Create New entries corresponding to existing Foreign Key database entries
I have a Table Books which has 100 entries. class Books(models.Model): name = models.CharField(max_length=30) I have another Table Titles which is newly created and has a Foreign Key relation. class Titles(models.Model): book = models.ForeignKey(Books, on_delete=models.CASCADE) .......... Can I automatically create 100 new entries for Titles based on the existing entry for Books? Or does changing Foreign Key to a different relation help? Please help me out or point me in the right direction. Thank you in advance. -
How to access user object in Django Djoser token serializer
I have the following in settings: DJOSER = { 'SERIALIZERS': { 'token': 'api.api.MyTokenSerializer' } } And here is that serializer, used for login: class MyTokenSerializer(TokenSerializer): auth_token = serializers.CharField(source="key") registration_complete = serializers.BooleanField(source="user.registration_complete", read_only=True) app_mode = serializers.CharField(source="user.app_mode", read_only=True) class Meta: model = djoser_settings.TOKEN_MODEL fields = ("auth_token", "profile_complete", "app_mode") and now I need to add one more field, but that field is a method field and it needs the user object. In pseudocode: class MyTokenSerializer(TokenSerializer): ... new_field = my_method(user_object) ... But I don't understand how to access the user object in this serializer. I see that the user object is used as a source in two fields (registration_complete and app_mode) but I don't know how to access it directly in order to pass it to the method. Is it possible to access the user object here? -
AJAX form submission executing multiple times - boostrap modal
I have a Django website with bootstrap modal with form. This is how I submit the form: $(function () { let clicked = 0; $('body').on('click', 'button.modal_submit', function (e) { clicked += 1; if (clicked <= 1) { e.preventDefault(); $.ajax({ url: "{% url 'insert_into_reference_table' ref_tab 1 %}", type: 'POST', data: $('#form1').serialize(), success: function (data) { alert(data); } }); } }); }); However, sometimes the success ajax function is executed twice (lets say in 1 of 30 submissions). Can anyone help me to find the solution? -
Django 1.11 - convert a datetime string with timezone 2018-01-01T00:00:00+03:00 into datetime object to be used for queryset
I am using Django 1.11 and Postgres 9.4. How can I convert this 2018-01-01T00:00:00+03:00 into a datetime object that can be used for queryset like below Tracking.objects.filter(created_at__gte=input_datetime) for Z time I can use this: input_datetime = datetime.datetime.strptime("2019-11-01T01:36:56.233032Z", "%Y-%m-%dT%H:%M:%SZ") but how can I make it work for this time (which seems to have timezone). I tried this but it didnt work. input_datetime = datetime.datetime.strptime('2018-01-01T00:00:00+03:00','%Y-%m-%dT%H:%M:%S.%f%z') Here is my model.py class Tracking(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) -
Django Memcache variable being set on server startup, not in view containing decleration
I am trying to use Memcache on Django and Heroku. I have followed the steps in the Heroku devcenter for setting up Memcache with Django (https://devcenter.heroku.com/articles/django-memcache). I only want to do a very simple .set() to add a variable to the cache. I set the cache variable in a view and have a print statement just to check it works, and I think what SHOULD happen is when that view is called, the cache variable is set. However, I see that when I start up my development server, the variable is set on startup, instead of when the view is called. Have I gone wrong in the setup somewhere? Is this how Memcache on Django is supposed to work? -
I not understand this code and dont know how to use it?
I took the code from the tutorial, and I need a professional look and description step by step views.py @login_required @transaction.atomic def update_profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST,instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, _('Your profile was successfully updated!')) return redirect('settings:profile') else: messages.error(request, _('Please correct the error below.')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'profiles/profile.html', { 'user_form': user_form, 'profile_form': profile_form }) -
Docker for django development auto update
Software used: Django 2.2.7 Docker 19.03.4 community OS = Ubuntu 18.04 I come from a background of vagrant where have previously set up an environment where I basically have a virtualenv lookalike but inside of a virtual machine. That is to say; I booted up the vagrant machine and using provisioning I installed all the required packages and requirements. Then on my host machine I had installed Eclipse and have the django project located. The last step for me was to bind these together using shared resources. This way I had ensured to always have up to date code. For those unfamiliar to Django; the runserver command is by default set to listen to code changes and "restarts" (not really) on every change immediately reflecting the change in the browser. Inside of the virtual machine I would then run the "runserver" command and thus being able to develop in such a way. Now using docker (I have to tie some software together and ultimately deploy using docker) I try to somewhat replicate this situation. I got my django docker up and running using a simple Dockerfile based off of python-3.7.5-stretch. The container runs fine as does django itself, however the … -
django with ajax: function not loading from source file
In my django project i'm trying to add a datepicker using jQuery. but when a load page in browser, the browser console gives error like "Uncaught TypeError: $(...).datepicker is not a function" but same ajax code i tried in simple html page then its working. why this datepicker() function is not loading in django from source location? <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title>Hello, world!</title> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <script> $( function() { $( "#datepicker" ).datepicker(); } ); </script> <p>Date: <input type="text" id="datepicker"></p> {% block contents %} {% endblock %} </body> </html> -
Return user if exists or create new
I'm trying to implement a view in Django/DRF that given a body containing facebook_id, first_name, last_name and email, the function checks if a user already exists in the database with either the email or the facebook_id. If not, then I create a new user and save it. I'm trying to do this with a serialzier and I'm not sure what the correct approach is. I want to use the same serializer I used for sign ups which is this: class ClientSerializer(serializers.ModelSerializer): class Meta: model = Client exclude = ['password'] It contains a lot of unnecessary fields that I wont need in this new view (such as phone number). Should I create a new seralizer just for this view? Also should I depend on the is_valid() function on the serializer to tell me if the user exists (if it raises an error) or is that too unreliable since the fields might be invalid? Here's what I have so far: @api_view(['POST']) def facebook_login(request): print(request.data) serializer = ClientSerializer(data=request.data) if serializer.is_valid(): # create a new user else: # possibly perform a get on the database to return the existing user? return Response(serializer.errors) -
How get information from three different tables at once?
I have following models: class Device(models.Model): name = models.CharField(max_length=100, blank=False) description = models.TextField(max_length=500, blank=True) ip_address = models.GenericIPAddressField(blank=True, null=True) contact_person = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) team = models.ForeignKey(Team, on_delete=models.SET_NULL, null=True) category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True) def __str__(self): return self.name class TimeSlot(models.Model): name = models.CharField(max_length=20) start_slot = models.CharField(max_length=10) end_slot = models.CharField(max_length=10) def __str__(self): return self.name class Reservation(models.Model): device = models.ForeignKey(Device, on_delete=models.CASCADE) time_slot = models.ForeignKey(TimeSlot, on_delete=models.CASCADE) date_of_reservation = models.DateField() user = models.ForeignKey(User, on_delete=models.CASCADE) def __str__(self): return "{} - {} for device: {} by {}.".format(self.time_slot, self.date_of_reservation, self.device, self.user) class ForbiddenSlot(models.Model): device = models.ForeignKey(Device, on_delete=models.CASCADE) time_slot = models.ForeignKey(TimeSlot, on_delete=models.CASCADE) def __str__(self): return str(self.time_slot) This is simple reservation system. I have problem to understand how create query for three different tables. I want get all TimeSlots which are not set in ForbiddenSlot and Reservation for given Device name.