Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Foreign Key Model Error
I Created Two models "Restaurantmenu" and "Cart", Where Restaurantmenu.Product_id is Primary key and Cart.prdct_id is Foreign key. Migration is done without any error. But when I insert data in the Cart then I got an error: ValueError: Cannot assign "'4359197'": "Cart.prdct_id" must be a "Restaurantmenu" instance. Find the Model File: class Restaurantmenu(models.Model): product_id = models.BigIntegerField(primary_key=True) shopid = models.CharField(max_length=50) prod_type = models.CharField(max_length=50,null=True) prd_category = models.CharField(max_length=50,null=True) prd_name = models.CharField(max_length=100,null=True) prd_amt = models.FloatField(null=True) prod_desc = models.CharField(max_length=200,null=True) offer = models.FloatField(null=True,default=0) offer_type= models.CharField(max_length=50,null=True,default='None') status = models.BooleanField(default=1) image = models.CharField(max_length=200) created_date = models.DateTimeField(auto_now_add=True) modified_date = models.DateTimeField(auto_now = True) def __unicode__(self): return self.product_id class Cart(models.Model): cart_id = models.BigIntegerField(null=True) cust_id = models.IntegerField(null=True) prdct_id = models.ForeignKey(Restaurantmenu,on_delete=models.DO_NOTHING) no_of_quantity = models.IntegerField(null=True) total_amt = models.FloatField(null=True) status = models.CharField(max_length=50,null=True) created_date = models.DateTimeField(auto_now_add=True) modified_date = models.DateTimeField(auto_now=True) def __unicode__(self): return self.cart_id -
Django Image field unable to change or remove existing image
I am trying to update an image in the Django Admin but when I check the 'clear' checkbox and save it, nothing happens. Also when I try to replace the existing image with a new nothing happens. The strange part is that I am using the exact code in a different model and its working fine there. Please note that the image gets saved to the desired location at the time of first creating the object in Django admin. But then when I try to edit it, nothing happens. My model looks something like this - class Community(models.Model): image = models.ImageField( upload_to=upload_location, null=True, blank=True, width_field="width_field", height_field="height_field", help_text="A image representing the Community") height_field = models.IntegerField(null=True, blank=True) width_field = models.IntegerField(null=True, blank=True) Also my upload_location function looks like this - def upload_location(instance, filename): return "community_banners/%s/%s" % (instance.name, filename) The things I have tried are - changing the upload location, deleting the model and recreating it. But nothing seems to work. I am using django 2.0 -
Excluding one row from a ManytoMany relationship in Django
This is my User object, class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(unique=True, max_length=255) mobile = PhoneNumberField(null=True) username = models.CharField(null=False, unique=True, max_length=255) is_superuser = models.BooleanField(default=False) is_active = models.BooleanField(default=False) And this is my Quiz object, class Quiz(Base): category = models.ForeignKey(Category, related_name='quizzes', on_delete=models.CASCADE) players = models.ManyToManyField(User) This is the filter that I'm using to find quizzes of a particular category that the user has not played, quiz = Quiz.objects.filter(category=category).exclude(player=user).order_by('created')[:1] But the exclude is a list and thus doesn't work. How do I correctly frame this query to exclude the current user? -
Autocomplete evererytime a word it's typed
I have my autocompleted method, but its only works for the first word I type, when I typed another word It doesn´t work, I want make my autocomple works everytime I add a new word. This is my function of autocomplete $(function() { $("#id_asesores").autocomplete({ source:"/inicio/api/get_asesores/", select: function (event, ui) { //item selected AutoCompleteSelectHandler(event, ui) }, minLength: 2, }); }); function AutoCompleteSelectHandler(event, ui) { var selectedObj = ui.item; } I am doing it in django 1.11.Also the autocomplete fuction is linke in a textarea.I think the problem is that my autocomplete function, get all the text tyoed id it, and searched it in the database, how can I make to only search the most recentlies typed word or words. -
User login in React JS
i am working on project , user can create a restaurant and add and manage meals and food , I try to display data from each user , my backend is written in django . In my API i am using JWT-Authentication to get the token like this : from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token urlpatterns = [ path('', TemplateView.as_view(template_name='react.html')), path('admin/', admin.site.urls), path('api/auth/jwt/', obtain_jwt_token), path('api/auth/jwt/refresh/', refresh_jwt_token), and i am using this endpoint in my react project like this : login(data){ const endpoint = '/api/auth/jwt/' const csrfToken = cookie.load('csrftoken') let thisComp = this if (csrfToken !== undefined) { let lookupOptions = { method: "POST", headers: { 'Content-Type': 'application/json', 'X-CSRFToken': csrfToken }, body: JSON.stringify(data), credentials: 'include' } fetch(endpoint, lookupOptions) .then(function(response){ return response.json() }).then(function(responseData){ console.log(responseData) thisComp.setState({ token: responseData.token }) let data = thisComp.state thisComp.refreshToken(data) }).catch(function(error){ console.log("error", error) }) } } After this i get the jwt token , i also set up a a customize jwt response payload to get back the token and the user, but now i don't know what can i do for display the data from the current user i wrote my view like this : class MealListCreateAPIView(generics.ListCreateAPIView): queryset = Meal.objects.all() serializer_class = MealSerializer lookup_field = 'restaurant.name' # permission_classes = … -
Status post. Django
I created an application where users can add posts. I would like to add the post status. Post after adding has the status "active." After 30 days, changes the status to "inactive" How can I do it? -
Getting a value from a different class in django
As new to both Python and Django I have encounterd what I think is a simple problem, but which does not want to go away. I have a "table" with factors obtained from class TjgFaktor(models.Model): typ = models.CharField(max_length=2) factor = models.FloatField() Next I have another class which is foreign-keyed to this: class Moment(models.Model): typ = models.ForeignKey(TjgFaktor,on_delete=models.SET_NULL,null=True) Now, what I want to do is to get the factor from the first class as an attribut to Moment. I have tried def factor(self): return TjgFaktor.objects.get(typ=self).factor in the hope of getting the correct factor. However, when I do something like person_moment = Moment.objects.all() for e in person_moment: print(e.factor()) what I get is "TjgFaktor matching query does not exist". So how should I do this? I guess it is the function: it works if I replace type=self with pk=1. -
django custom database engine documentation
I'm making a django project where there is the need to use information from an ApiRest. To manage the information more easily I created classes to get the info and display it. To make it more easy to use relations and use the django system I want to use a custom database backend instead of the classes. But there I cant find any documentation on how it should be done. Is there a place where I can find the documentation of how a database engine works in django? -
Celery keeps task in memory after finish
I am using Django with Celery to offload long-running tasks. This all works beautifully, except that when a task is done, Celery seems to keep data in memory. @shared_task def export_xlsx(): tmpfile = settings.FILE + ".tmp" with open(tmpfile, "w") as datafile: datafile.write(to_xlsx()) os.rename(settings.FILE + ".tmp", settings.FILE) return True the to_xlsx() function returns an Excel workbook as string using save_virtual_workbook from openpyxl.writer.excel. And it seems like this string is never released from memory: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 26536 bf 20 0 2763256 1.921g 9220 S 0.0 25.0 4:45.69 celery 26538 bf 20 0 1550080 798892 7336 R 88.0 9.9 0:08.20 celery PID 26536 is a task that was already finished. 26538 is one that is running. -
Django ORM order by produces unordered result
I have a model with a submission date field. Sorting by this field is producing inconsistent results, and I'm having a tough time figuring out why. The back end database is Postgres. Here is the relevant part of the model: class Article(models.Model): headline_text = models.CharField(max_length=255, verbose_name='Headline') sub_date = models.DateField('date submitted') sub_timestamp = models.DateTimeField('Date and Time submittted') When saving, I save data to these fields and I can see that the data is there. In the template later, you'll see too. Now in the view, I am passing a sort order allowing the user to choose how to sort their information: def article_index(request): pgNm = int(request.GET.get('p', 1)) sortSelect = request.GET.get('s', None) isRev = request.GET.get('r', None) if( (sortSelect is None) or(sortSelect=='d')): if(isRev == 't'): qset = Article.objects.order_by('sub_timestamp') else: qset = Article.objects.order_by('-sub_timestamp') else: qset = Article.objects.order_by('-sub_date') p = Paginator(qset, 24) pg = p.page(pgNm) article_list = pg.object_list #article_list = qset return render(request, 'article/index.html', {'article_list': article_list, 'pgNm': pgNm, 'hasNext': pg.has_next(), 'nextPg':pgNm + 1, 'hasPrev': pg.has_previous(), 'prevPg': pgNm - 1}) The commented out line there, article_list=qset, was me trying to see if Paginator was causing this, but it produced the same results. Now, here is the relevant part of the template: {% for article in article_list.all … -
Add BrinIndex to existing PostgreSQL database using Django
I am running a website using Django, of which the database runs on PostgreSQL. To increase performance I want to add a BrinIndex to a model that is naturally ordered on disk. I have added the index to my models definition as follows. class Measurement(models.Model): class Meta: indexes = ( BrinIndex(fields=['time']) ) time = models.DateTimeField( 'Time of measurement', null=True ) But running python3 manage.py makemigrations Returns an error Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm 2018.1.1\helpers\pycharm\django_manage.py", line 52, in <module> run_command() File "C:\Program Files\JetBrains\PyCharm 2018.1.1\helpers\pycharm\django_manage.py", line 46, in run_command run_module(manage_file, None, '__main__', True) File "C:\Python36\lib\runpy.py", line 205, in run_module return _run_module_code(code, init_globals, run_name, mod_spec) File "C:\Python36\lib\runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "C:\Python36\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:/Users/*USERNAME*/*PROJECT_NAME*\manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "C:\Users\*USERNAME*\*PROJECT_NAME*\venv\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line utility.execute() File "C:\Users\*USERNAME*\*PROJECT_NAME*\venv\lib\site-packages\django\core\management\__init__.py", line 347, in execute django.setup() File "C:\Users\*USERNAME*\*PROJECT_NAME*\venv\lib\site-packages\django\__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "C:\Users\*USERNAME*\*PROJECT_NAME*\venv\lib\site-packages\django\apps\registry.py", line 112, in populate app_config.import_models() File "C:\Users\*USERNAME*\*PROJECT_NAME*\venv\lib\site-packages\django\apps\config.py", line 198, in import_models self.models_module = import_module(models_module_name) File "C:\Python36\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 978, in _gcd_import File "<frozen importlib._bootstrap>", line 961, in _find_and_load File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked File "<frozen … -
Redirect in django admin with changelist_view
I would like to redirect in django admin panel from: http://127.0.0.1:8000/admin/events/event/ to: http://127.0.0.1:8000/admin/events/event/?date__lt=2018-6-11 But I got an error: Reverse for 'events_event_changelist' with keyword arguments '{'date__lt': datetime.date(2018, 6, 11)}' not found. 1 pattern(s) tried: ['admin/events/event/$'] Here is my code: def changelist_view(self, request, extra_context=None): if not request.method != 'GET': print(datetime(2018, 6, 11)) url = reverse('admin:events_event_changelist', kwargs={'date__lt': date.today()}) return HttpResponseRedirect(url) Please tell me where I made mistake -
How to inspect raw SLQ queries generated by Django when debugging?
I am trying to profile a large portion of my project, carefully analyzing queries generated by each line of code. Careful analysis requires running the server in debug mode so I can step over these lines one by one and take my time to study the queries. So far I have tried three unsucessful ways of doing this: Inspecting django.db.connection.queries You can see what queries have been generated and executed against the database by inspecting the django.db.connection.queries dictionary. The problem with this: It needs to be actively inspected. You cannot just have it as a listener and log these queries behind the scenes. You need to actually print out connection.queries after every single line, which is a nuisance. Apparently it only stores queries generated in the same file where you are using connection.queries. If you inspect it right after calling some_method() and that method is imported from some other place, you will not see the queries some_method generated. Logging configuration I set up the following logging configuration in my settings file that logs all queries run against the database: LOGGING = { 'version': 1, 'filters': { 'require_debug_true': { '()': 'django.utils.log.RequireDebugTrue', } }, 'handlers': { 'console': { 'level': 'DEBUG', 'filters': ['require_debug_true'], … -
Django 2.0.6 development server crashes on external connection
I've got some problems with the development server of Django 2.0.6. It keeps running until I try to connect to it from another client, then it crashes immediately. DEBUG = True is set in the settings.py Im using Python 3.5.3 (default, Jan 19 2017, 14:11:04) python3 manage.py runserver 0.0.0.0:8000 Performing system checks... System check identified no issues (0 silenced). June 11, 2018 - 14:32:19 Django version 2.0.6, using settings 'bidm_django.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C. ---------------------------------------- Exception happened during processing of request from ('10.29.0.178', 61797) Traceback (most recent call last): File "/usr/lib/python3.5/socketserver.py", line 625, in process_request_thread self.finish_request(request, client_address) File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python3.5/socketserver.py", line 681, in __init__ self.handle() File "/home/srojek/venv/lib/python3.5/site-packages/django/core/servers/basehttp.py", line 139, in handle self.raw_requestline = self.rfile.readline(65537) File "/usr/lib/python3.5/socket.py", line 576, in readinto return self._sock.recv_into(b) ConnectionResetError: [Errno 104] Connection reset by peer ---------------------------------------- pip freeze Django==2.0.6 pkg-resources==0.0.0 pytz==2018.4 While it works fine locally on a windows machine using the pyCharm IDE it crashes on the linux machine. -
Python; Instagram API, get user feed
There are several similar questions but they all either have outdated answers or were left unanswered. I am trying to use the instagram API to get a user's feed, but this the only related API call I can find is: https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS-TOKEN and this only returns posts from the user. Is it still possible to do this (after the user is authenticated, of course)? Any help would be greatly appreciated, even a simple yes or no. -
Pass data to template outside view in django?
I mean, i have the classic always-present side panel where i would like to display some model data (eg. year/months for existing posts publication date, tags and alike). Since this side panel is always present i also would like to avoid putting the retrieving logic in every view i define since it is repetitive, error/bug prone, less mantainable, etc. Is there a way in django to define such logic and bind/inject its result in every view i made? Even better it would be to do this directly in the template to decouple it from the views at all, so if i got a different page that don't need the data (like a login/out page) django won't waste work/time to retrieve/manipulate something that won't be even displayed. thanks in advance -
How to apply a function to option selected tag?
I have the following dropdown: <select id="role" name="role" class="form-control"> <option selected>Choose an option</option> {% for index, value in roles_form.fields.roles.choices %} <option value="{{ index }}">{{ value }}</option> {% endfor %} </select> And I have a function that show and hides another div when the user chose between the options: $('select').on('change', function() { var developer = {{ developer_id }}; var value = $(this).val(); console.log(developer); if (value == developer) { $('#github_link').show(); } else { $('#github_link').hide(); } }) So, when the user choose Developer the div remains shown, and when they choose any other option this div hides. Everything works fine, but there's a thing, when the page loads this tag: <option selected>Choose an option</option>mantain the div shown, and that's not correct, when the page loads the div needs to be hidden too. However I'm not pretty sure how to add that to the function, any thoughts? -
Django /w MySql : Query data according to the foreign Key ( Filter / get / get_object_or_404( ))
I started Django since 2 weeks and I try to obtain information of the table 'Salary' with regard to the pk ' matriculeemp ' selected in the url,'matriculeemp' is the foreign key of 'Salary' and the pk of the table employe. I was not able to to find the answer by looking on other post of stackoverflow nor by posting mine on other forums, thus I come to you. I have these models and this view, these paths in my urls.py of my folder of application : class Salaire(models.Model): date = models.DateField(db_column='Date') charge_patronale_mensuel = models.FloatField(db_column='Charge_Patronale_Mensuel', blank=True, null=True) charge_salariale_mensuel= models.FloatField(db_column='Charge_Salariale_Mensuel', blank=True, null=True) autre_charge_mensuel_fixe = models.FloatField(db_column='Autre_Charge_Mensuel_Fixe', blank=True, null=True) salaire_brut_mensuel = models.FloatField(db_column='Salaire_Brut_Mensuel', blank=True, null=True) matriculeemp = models.ForeignKey(Employe, models.DO_NOTHING, db_column='MatriculeEmp', blank=True, null=True) salaire_net_mensuel = models.FloatField(db_column='Salaire_Net_Mensuel', blank=True, null=True) charge_variable_mensuel = models.FloatField(db_column='Charge_Variable_Mensuel', blank=True, null=True) class Meta: db_table = 'salaire' ordering = ["date"] class Employe(models.Model): matriculeemp = models.AutoField(db_column='MatriculeEmp', primary_key=True) nomemp = models.CharField(db_column='NomEmp', max_length=15, blank=True, null=True) prenomemp = models.CharField(db_column='PrenomEmp', max_length=15, blank=True, null=True) datenaissanceemp = models.DateField(db_column='DateNaissanceEmp', blank=True, null=True) dateembaucheemp = models.DateField(db_column='DateEmbaucheEmp', blank=True, null=True) genreemp = models.CharField(db_column='GenreEmp', max_length=10, blank=True, null=True) codefonc = models.ForeignKey('Fonction', models.DO_NOTHING, db_column='CodeFonc', blank=True, null=True) # Field name made lowercase. codecateg = models.ForeignKey(Categorie, models.DO_NOTHING, db_column='CodeCateg', blank=True, null=True) # Field name made lowercase. codeville = models.ForeignKey('VilleEmp', models.DO_NOTHING, db_column='CodeVille', blank=True, … -
Is there a way to show more details about a Relational Field in Django REST Framework JSON API
I'm trying to use the Django REST Framework JSON API and was wondering if there is a way to serialize Relations (ForeignKey and all) with a bit more detail compared to : "post": { "type": "Post", "id": 1 } I was thinking of something along the lines of : "post": { "type": "Post", "id": 1, "name": "First Post" "tag": "Development" } -
setting django and angular with docker
I have written my first Django 2.0 application and using Angular for frontend. My Directory structure looks like app |-- config # nginx configuration files |-- nginx |-- nginx.conf |-- angular-app # frontend angular application |-- angular.json |-- package.json |-- src # Django backend application |-- myapp |-- urls.py |-- wsgi.py |-- manage.py |-- static_cdn # store and host static files and uploaded files |-- static_root |-- media_root |-- .dockerignore |-- docker-compose.yml |-- Dockerfile |-- requirements.txt |-- start.sh The content of docker-compose.yml version: '3' services: nginx: image: nginx:latest container_name: "myapp-nginx" ports: - "10080:80" - "10443:43" volumes: - .:/app - ./config/nginx:/etc/nginx/conf.d depends_on: - web web: build: . container_name: "myapp-dev" command: ./start.sh volumes: - .:/app ports: - "9010:9010" depends_on: - db db: image: postgres container_name: "myapp-postgres-db" content of Dockerfile FROM ubuntu:18.04 FROM python:3 ENV PYTHONUNBUFFERED 1 ENV LC_ALL C.UTF-8 ENV LANG C.UTF-8 # -- Install Application into container: RUN set -ex && mkdir /app WORKDIR /app ADD requirements.txt /app/ RUN pip install -r requirements.txt # -- Adding dependencies: ADD . /app/ content of start.sh #!/usr/bin/env bash python3 src/manage.py makemigrations python3 src/manage.py migrate echo --: Running collectstatic python3 src/manage.py collectstatic --noinput echo --: Starting Gunicorn. gunicorn --pythonpath src myapp.wsgi:application \ --bind 0.0.0.0:9010 \ … -
Django is changing the order of my Integer List
So I'm facing the following problem. When creating a List of Integers in Android Studio: List<Integer> myIDs= new ArrayList<>(); I'm putting the integers (id's) in a given order, with a result of, p.e: myIds = [69, 65, 67] The problem is that when I'm sending the List to my django server it is ordering it by itself to: "myIds ": [ 65, 67, 69 ] Any idea on why is that happening, and how could I solve it? On the server, this is my models.py ... myIds = models.ManyToManyField('garments.Garment') ... Thank you in advanced! -
how to print forms.ValidationError in django
found this in a tutorial but "password and confirm_password does not match" is not printing to html page. from django import forms from django.contrib.auth.models import User class UserForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput) confirm_password = forms.CharField(widget=forms.PasswordInput()) first_name = forms.CharField(max_length=30) last_name = forms.CharField(max_length=30) email = forms.EmailField(max_length=254) class Meta: model = User fields = ['username', 'first_name', 'last_name', 'email', 'password','confirm_password'] def clean(self): cleaned_data = super(UserForm, self).clean() password = cleaned_data.get("password") confirm_password = cleaned_data.get("confirm_password") if password != confirm_password: raise forms.ValidationError( "password and confirm_password does not match" ) -
Implementing User Authentication in Django with MongoDB
I am trying to implement User Authentication with MongoDB in Django. I am currently trying to use https://nesdis.github.io/djongo/ to implement this, but have been unsuccessful so far. I was wondering if anyone knows of a tutorial or the steps required to complete this? -
limit the choices for objects related to the chosen in the select
I wonder if anyone would know a way to return the categories related to the session chosen in the select options. Example: class Session(models.Model): name = models.CharField("Sessão", max_length=100) slug = models.SlugField("Slug", max_length=100) class Category(models.Model): session = models.ForeignKey(Session, verbose_name='Sessão', on_delete=models.CASCADE, max_length=100, default=1, blank=True, null=True) category = models.CharField("Categoria", max_length=100) slug = models.SlugField("Slug", max_length=100) class Article(models.Model): category = models.ForeignKey(Category, verbose_name='Categoria', on_delete=models.CASCADE, max_length=100, default=1, blank=True, null=True) session = models.ForeignKey(Session, verbose_name='Sessão', on_delete=models.CASCADE, max_length=100, default=1, blank=True, null=True) When registering an Article, I would define a session for the article, and then I would like to return categories, only the categories related to that session. Can anybody help me ? I believe this would have to be dynamically ... Strong hug! -
Django-filters: multiple IDs in a single query string
Using django-filters, I see various solutions for how to submit multiple arguments of the same type in a single query string, for example for multiple IDs. They all suggest using a separate field that contains a comma-separated list of values, e.g.: http://example.com/api/cities?ids=1,2,3 Is there a general solution for using a single parameter but submitted one or more times? E.g.: http://example.com/api/cities?id=1&id=2&id=3 I tried using MultipleChoiceFilter, but it expects actual choices to be defined whereas I want to pass arbitrary IDs (some of which may not even exist in the DB).