Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
What's the best practice to update a field automatically?
Here's what I want to accomplish: I have an 'age' field in my model which I'd like its value to be the time duration up til now - and I mean now as in real time, not just the obj creation time or updated time. At first I was wondering if there's some kind of a dynamic field in django that the field value would just keep updating based on a formula. But then I figured that wasn't realistic, it has to be updated by something at some point in time. Right now what comes on my mind is to write a model method to calculate the value and assign it to the field, and to manually calls it and write the new value to the field everytime before the query of the class objects. I wonder is there any better way to accomplish this since my method will add some loading to the database which might just be unnecessary. Thanks for your help. -
django JSONField contains greater than
I have the model Item with JSON field metadata = JSONField(null=True) sample content: [{"value": 50, name: "other"}] How can I query all rows where the "value" is larger than 50? I tried Item.objects.filter(metadata__contains__value__gt=50) I've managed to query an exact value with Item.objects.filter(metadata__contains=[{"value": 50}]) -
how to get checkbox values from ajax in django?
here i have some table and there is a checkbox and i want to send email to all the checked data of that table.For this i have tried this.But this is not working .The problem is while getting the list of checked items.I am not getting any checked item.How can i solve this ? views.py def send_mail_selected_contact(request): selected_contact = ContactForm.objects.filter(id__in=request.POST.getlist('messages')) print(selected_contact) form = SendMailContact(request.POST or None) if form.is_valid(): subject = form.cleaned_data['subject'] message = form.cleaned_data['message'] for contact in selected_contact: send_mail(subject, message, ' <settings.EMAIL_HOST_USER>', [contact.email]) messages.success(request, 'Mail Sent') return redirect('admin:contact_message') template <tbody> {% for contact in contacts %} <tr> <td> <input name ="messages" type="checkbox" id="contact-{{contact.id}}" value={{contact.id}}"> <label for="contact-{{contact.id}}">{{ forloop.counter }}</label> </td> <td>{{ contact.name }}</td> <td>{{ contact.email }}</td> <td>{{ contact.subject }}</td> <td>{{ contact.message }}</td> </tr> {% endfor %} </tbody> //modal <button class="btn btn-primary" type="button" data-toggle="modal" data-target="#item-unit-modal1"> Send Mail To Selected Email </button> <div class="modal" id="item-unit-modal1"> <div class="modal-body"> <form method="POST" action="{% url 'admin:send_mail_selected_contact' %}" class="unit-ajax-form"> {% csrf_token %} <p><label>Subject</label> <input type="text" name="subject" class="form-control required" placeholder="Enter subject" ></p> <p><label>Message</label> <textarea name="message" class="form-control required" placeholder="Enter message" ></textarea></p> <button class="btn btn-primary mt-30">Send Mail</button> </form> </div> ajax var messages = new Array(); $("input:checked").each(function() { messages.push($(this).val()); }); $.ajax({ url:'admin/selected/send/mail/contact/', data:{ name: name, messages: messages, } }); -
how to count the number of cascaded item if ever delete happens in django orm
I want to mass delete in ORM django level but i want to see or simulate the impact of cascaded instance that connected to it. Thanks in advance -
error when running an app "OSError: [WinError 123] The filename, directory name"
When I run "python manage.py runserver", I get error which say "OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '' ". before it was working fine but now when I run every app it gives me same error This is for window 10, running python 3.6.8, django 2.2.1, I have tried uninstalling python and install again but still the problem exist. All I know is that there is a new system check and how django process views but I don't know how will I rectify the problem and where to go change some code if I have to. -
User and superuser gets deleted automatically
I am working on a project where I am using AbstractUser class of django. I have already created the superuser. Whenever I create a new user either from interface or from admin, all the existing users get deleted including super user. I am not able to find any bugs in my code. models.py : from django.db import models from django.conf import settings from django.contrib.auth.models import AbstractUser from datetime import date class CustomUser(AbstractUser): employee_id = models.IntegerField(primary_key=True,default=0) dateofbirth = models.DateField(default=date.today) contact = models.CharField(max_length=10,default="No contact") address = models.CharField(max_length=300,default="No address given") image = models.ImageField(default=None) manager_id = models.IntegerField(default=0) date_of_joining = models.DateField(default=date.today) date_of_termination = models.DateField(default=date.today) status = models.CharField(max_length=8,default="inactive") role_id = models.IntegerField(default=0) def __str__(self): return self.username views.py from django.contrib.auth import login, authenticate from django.contrib.auth.forms import UserCreationForm from .forms import CustomUserCreationForm from django.shortcuts import render, redirect def signup(request): if request.method == 'POST': form = CustomUserCreationForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') raw_password = form.cleaned_data.get('password1') user = authenticate(username=username, password=raw_password) login(request, user) return redirect('emp_dashboard') else: form = CustomUserCreationForm() return render(request, 'signup.html', {'form': form}) forms.py from django import forms from django.contrib.auth.forms import UserCreationForm, UserChangeForm from .models import CustomUser class CustomUserCreationForm(UserCreationForm): class Meta(UserCreationForm): model = CustomUser fields = ('username', 'email') class CustomUserChangeForm(UserChangeForm): class Meta: model = CustomUser fields = ('username', 'email') settings.py … -
ajax url and get is not working in django?
please look into below code AJAX FUNCTION <script> $(document).ready(function() { $("#id_module").on('change', function(){ var mod1 = $(this).val(); alert(mod1); $.ajax({ url: 'submodule/'+ mod1, type:'GET', dataType:'json', success: function(response){ alert(JSON.stringify(response)); submod=response['submod']; alert(submod); $('#submodule').empty(); $("#submodule").prepend($('<option>', { value: '', text: '-- Select Sub Module Type --' })); $.each(submod, function(ind){ $("#submodule").append($('<option>', { value: submod[ind]['sub_module'], text: submod[ind]['sub_module'] })); }); $('#submodule').selectpicker("refresh"); } }); }); }); </script> My Django -- URL: re_path(r'^eForm/submodule/(P<n_moduleid>\d+)$',views.submodule,name='submodule'), My Django --Views: def submodule(request,n_moduleid): try: if request.method=='GET': submod=[] submod=TblTxEticketdetails.objects.using('ETicketing').values('sub_module').filter(Q(module_id=n_moduleid)).distinct() else: messages.error(request, 'Error Occurred!!!') data = {'submod': list(submod)} return JsonResponse(data, safe=False) except Exception as e: messages.error(request, "Error Occured!!!") This is my first time pasting question in stack overflow.I think i have messed up posting my question. please fell free to ask questions regarding code i have gone through all of my code and i couldn't find where my code is wrong. while running, alert box appears but it does not go to ajax function. i need little help please!!! -
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' on Google App Engine
I'm trying to connect to Google Cloud MySQL from Google App engine but getting OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)") I've tried using following settings: app.yaml: runtime: python37 env: standard handlers: - url: /static static_dir: static runtime_config: python_version: 3 db connection string: mysql+pymysql://{user}:{password}@localhost/{db}?unix_socket=/cloudsql/{conn_name} My concern is it was working before but when I changed the db user/password it started giving me above error and reverting my last change did not solved my problem. Is there some issue with my settings or could it be some cache issue on app engine? -
No new migration for subclass of subclass of a field
I have the following setup (Django 2.0.6, also in 2.2), the first migration is with the field having max_length=64 and now I want to change the DummyCharField.max_length to 255: class BaseDummyCharField(models.CharField): def __init__(self, *args, **kwargs): if 'max_length' not in kwargs: kwargs['max_length'] = 64 super().__init__(*args, **kwargs) class DummyCharField(BaseDummyCharField): def __init__(self, *args, **kwargs): kwargs['max_length'] = 255 super().__init__(*args, **kwargs) class DummyModel(models.Model): dummy = DummyCharField() When running makemigrations, it just says "No changes detected". I also tried using deconstruct() as told in the docs, but it still didn't work. class DummyCharField(BaseDummyCharField): def __init__(self, *args, **kwargs): kwargs['max_length'] = 255 super().__init__(*args, **kwargs) def deconstruct(self): name, path, args, kwargs = super().deconstruct() del kwargs['max_length'] return name, path, args, kwargs As a workaround I made the following: class DummyCharField(BaseDummyCharField): def __init__(self, *args, **kwargs): # If wrapped inside an `if`, it works... if 'max_length' not in kwargs: kwargs['max_length'] = 255 ... Am I missing something here or what exactly is my fault in this case? -
Scatter plot in Django app using matplotlib and mpld3
I am trying to create a simple graph in my django app and for that I am using matplotlib and mpld3. This is the code below: views.py fig, ax = plt.subplots() ax.scatter([1, 10], [5, 9]) html_graph = mpld3.fig_to_html(fig) return render(request, 'dispatcher_output.html', {'graph': [html_graph]}) html {% for elem in graph %} {{elem|safe}} {% endfor %} but I am getting this error: TypeError: Object of type ndarray is not JSON serializable How do I solve this ? -
Calculating percentage and other metric per day (last 30 days) django ORM
I am new to Django and trying to calculate the percentage and some metrics. The issue with the approach is that I am not able to get the data by grouping it per day. Here is my solution. Please suggest a better way. def calculate_percentage(neumerator, denominator): percentage = (neumerator / denominator) * 100 return percentage def test_percentage(): percentage_metric_details = [] counter = 1 today_ISO = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%dT%H:%M:%S') while counter < 6: todays_first_bucket_date = datetime.datetime.now() second_bucket_start = todays_first_bucket_date - timedelta(days=counter) last_ISO = datetime.datetime.fromtimestamp(second_bucket_start.timestamp()).strftime('%Y-%m-%dT%H:%M:%S') last_ISO = str(last_ISO).split("T") last_ISO = last_ISO[0] + " 00:00:00.000000" print("todays first bucket", todays_first_bucket_date) print("start date", today_ISO) print("last date", last_ISO) counter = counter + 1 total_count = ArticleStatus.objects.filter( created_on__lte=today_ISO, created_on__gte=last_ISO).count() total_count_approved = ArticleStatus.objects.filter( created_on__lte=today_ISO, created_on__gte=last_ISO, reasonForReject="accepted").count() total_pending_count = ArticleStatus.objects.filter( created_on__lte=today_ISO, created_on__gte=last_ISO, reasonForReject="").count() print(total_count_approved) accept_percentage = calculate_percentage(total_count_approved, total_count) pending_percentage = calculate_percentage(total_pending_count, total_count) percentage_metric_details.append({ "total_count": total_count, "approved_percentage": accept_percentage, "pending_percentage": pending_percentage }) todays_first_bucket_date = last_ISO today_ISO = last_ISO return percentage_metric_details z = test_percentage() print(z) -
Adding nested data using Elasticsearch DSL
I have 2 models that looks like this in Django: class Poll(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) question = models.CharField(max_length=200) class Choice(models.Model): question = models.ForeignKey(Poll, on_delete=models.CASCADE) choice = models.CharField(max_length=120) vote_count = models.IntegerField(default=0) Every time a user adds a new poll, it should save into Elasticsearch. The way it should save should look like this: { "question": "Dogs or cats?", "choices": [ { "choice": "Dogs" }, { "choice": "Cats" } ] } Within my Poll model, I create a function named indexing like so: def indexing(self): obj = PollIndex( meta={'id': self.id}, question=self.question, choices=self.choice_set, ) obj.save() return obj.to_dict(include_meta=True) I define the index like so: class PollIndex(Document): question = Text() choices = Nested() However, I am getting an error that says: AttributeError: 'RelatedManager' object has no attribute 'items' What am I doing wrong? -
Access python dic in template view with Django
When I want to pass a dic from views.py to a template it is not rendered correctly. I tried using |safe and JSON.parse(), however both did not solve the issue. It seems like the error could already happen in views.py. views.py participant_per_challenge = {} for participant in challenge_participants: if str(participant.challenge.id) in participant_per_challenge: participant_per_challenge[str(participant.challenge.id)].append(participant.player.userid) else: participant_per_challenge[str(participant.challenge.id)] = [] participant_per_challenge[str(participant.challenge.id)].append(participant.player.userid) my_dict = { 'challenges' : challenges, 'challenge_participants' : challenge_participants, 'unique_participants': unique_participants, 'participant_per_challenge': participant_per_challenge, } return render(request, "challengeview/index.html", context=my_dict) here an example dic from views.py: { "1": [ "bc5ac2b77a2d4b9e90ff4aa6012a4891", "a61254636e5f432292459b406cf55f47" ], "2": [ "a61254636e5f432292459b406cf55f47", "bc5ac2b77a2d4b9e90ff4aa6012a4891" ] } index.html <script type="text/javascript"> var player_per_challenge = "{{ participant_per_challenge|safe}}"; console.log(typeof player_per_challenge); //THIS RETURNS STRING </script> expected result: a JSON object of which I can access parameters actual result: a string. JSON.parse() did not work as well ("Uncaught SyntaxError: Unexpected token with JSON.parse"). Thank you very much for your help! -
"HSH.Usuario.email: (models.E006) The field 'email' clashes with the field 'email' from model 'HSH.usuario'"
I'm trying to create a custom user model so that I can have three different types of user for my site. I'm using django 2.2.1, which I have been taking a very rushed crash course on for the last few months, so admittedly I'm not very good at it quite yet, and most of my code is the result of me following other's examples on online courses or stackoverflow. With that said, following one of these courses (speciffically coding for entrepeneur's take on custom user models), I tried to do this by inheriting the AbstractUser class and adding in the flags, and a foreign key to a different model because types of user needs to have extra info stored about them. # From HSH.models.py class Usuario(AbstractUser): es_admin = models.BooleanField(default=False) es_premium = models.BooleanField(default=False) email = models.EmailField(unique=True) id_perfil = models.ForeignKey(Perfil, on_delete=models.CASCADE) USERNAME_FIELD = email # From settings.py AUTH_USER_MODEL = 'HSH.Usuario' However, when I attempt to makemigrations, I get the following error message: SystemCheckError: System check identified some issues: ERRORS: HSH.Usuario.email: (models.E006) The field 'email' clashes with the field 'email' from model 'HSH.usuario'. This error continues to occur even if I change the field's name. SystemCheckError: System check identified some issues: ERRORS: HSH.Usuario.mail: … -
Django Cache: How to ignore browser-specific variations in the HTTP_ACCEPT header?
While using a DatabaseCache in Django 2.1.7. how can I generate the cache key only on the basis of the URL, ignoring small differences in the HTTP_ACCEPT header? In Safari the HTTP_ACCESS header's Value is 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' but in Chrome it is 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' Since _generate_cache_key() in utils/cache.py uses the Value of HTTP_ACCESS header to generate the cache key, these are saved as two distinct cache keys and the view has to call the original SQL queries twice over, instead of using the cached value. I want to cache this in a way that the same cached response is served for the same URL for all browsers that accept 'text/html'. However I don't want to ignore HTTP_ACCEPT entirely, as I still want 'text/json' to be cached separately so as to not interfere with django-rest-framework. -
Django - "with / as" to fill block is not working
I am trying to not repeat myself in Django, but use the same content for 2 blocks. The content is static so I would rather have in in my templates than sending them through views. This was the solution I found : {% with "My title" as title %} {% block TitleOne%}{{ title }}{% endblock TitleOne %} {% block TitleTwo %}{{ title }}{% endblock TitleTwo %} {% endwith %} {% endwith %} But it does not work. If I write it like : {% block TitleOne%}"My title"{% endblock TitleOne %} {% block TitleTwo %}"My title"{% endblock TitleTwo %} {% endwith %} It works perfectly. But of course it s not DRY. If I write it like : {% with "My title" as title %} {% block TitleOne%}"My title"{% endblock TitleOne %} {% block TitleTwo %}{{ title }}{% endblock TitleTwo %} {% endwith %} {% endwith %} Only the 1st one displays right. But not DRY as well. (I am using Django 2.2, Python 3.7.) -
int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method'
hi folks am trying to call the model from Organiser model in sponsor model and EventDetails from organizer model is referred to sponsor model while fatching the primary key of eventDetail i am not able to get views.py if request.method =='POST': eventDetail =EventDetails() if EventDetails.objects.filter(pk=id,event=request.POST['event_title']).exists(): event_id =eventDetail.id sponsor_ship=SponsorShipDetails(event_id=event_id,event_title=event_title,platinum_sponsor=platinum_sponsor,f_platinum=f_platinum,ex_platinum=ex_platinum,gold_sponsor=gold_sponsor, f_gold=f_gold,ex_gold=ex_gold,silver_sponsor=silver_sponsor,f_silver=f_silver,ex_silver=ex_silver) sponsor_ship.save() model.py fromORganiser class EventDetails(models.Model): event = models.CharField(max_length=100) no_participant = models.IntegerField() event_level = models.CharField(max_length=100) eligibility = models.CharField(max_length=100) prerequisite = models.TextField(max_length=1500) facility = models.CharField(max_length=100) event_detail_docs = models.FileField(upload_to='event_details_docs') class SponsorShipDetails(models.Model): event_id =models.OneToOneField(EventDetails,on_delete=models.CASCADE,primary_key=True) event_title = models.CharField(max_length=100,default=True) platinum_sponsor =models.CharField(max_length=100) f_platinum = models.TextField(max_length=1500) ex_platinum=models.IntegerField() gold_sponsor =models.CharField(max_length=100) f_gold =models.TextField(max_length=1500) ex_gold = models.IntegerField() silver_sponsor= models.CharField(max_length=100) f_silver=models.TextField(max_length=1500) ex_silver = models.IntegerField() TypeError at /Organizer/sponsorShipDetails int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method' Request Method: POST Request URL: http://localhost:8000/Organizer/sponsorShipDetails Django Version: 2.1 Exception Type: TypeError Exception Value: int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method' Exception `enter code here`Location: D:\Workspace\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 965 Python Executable: D:\Workspace\python.exe Python Version: 3.7.1 -
How can I display the image in django admin-panel [duplicate]
This question already has an answer here: Django Admin Show Image from Imagefield 8 answers I am new to django. and trying to view the image in admin panel.I have no idea how to view the image in admin-panel. thanks in advance I have this model: class Post(models.Model): title = models.TextField() cover = models.ImageField(upload_to='images/') def __str__(self): return self.title -
Error pagination when filter to list by category ListView Django
I'm new with programming, my mentor only youtube, and when i got error, i search similar error in stackoverflow, but no luck now. I have a problem with my code or maybe my logic. I don't have much experience or knowledge about call queryset for filtering correctly. This my urls.py urlpatterns = [ path('', IndexView.as_view(), name='index'), path('product/<int:page>', ProductListView.as_view(), name='allproducts'), path('<categories>/<int:page>', ProductCategoryListView.as_view(), name='product_category'), ] I have ProductListView with no error pagination. ProductListView(ListView): class ProductListView(ListView): context_object_name = 'product_list' template_name = 'product.html' model = Product paginate_by = 16 def get_context_data(self, *args, **kwargs): context = super(ProductListView, self).get_context_data(*args,**kwargs) context ['catlist'] = Category.objects.all() return context when i filter by category, pagination doesn't work. filter by category work fine, but when paginate_by more than my product, page error Reverse for 'product_category' with arguments '('', 2)' not found. 1 pattern(s) tried: ['(?P[^/]+)/(?P[0-9]+)$']\ class ProductCategoryListView(ListView): context_object_name = 'product_list' template_name = 'product_list.html' model = Product paginate_by = 20 def get_queryset(self): self.queryset = self.model.objects.filter(category__name=self.kwargs['categories']) return super().get_queryset() def get_context_data(self, *args, **kwargs): context = super(ProductCategoryListView, self).get_context_data(*args,**kwargs) context ['catlist'] = Category.objects.all() return context This my models.py class Category(models.Model): name = models.CharField(max_length=125) image = models.ImageField(upload_to=upload_location) class Product(models.Model): name = models.CharField(max_length=125) category = models.ForeignKey(Category, null=True, on_delete=models.DO_NOTHING) image = models.ImageField(upload_to=upload_lokasi) slug = models.SlugField(blank=True, editable=False) active = models.BooleanField(default=True) … -
How to solve this "Object of type bytes is not JSON serializable"? I am new to python n django
I am doing a search test and I always get this "Object of type bytes is not JSON serializable" error. I an new to Python and Django and am learning coding now. Couldn't find where I went wrong. stk_navigation.html <div class="ml-auto"> {% search_box request %}</div> search_tags.py @register.inclusion_tag("tags/search_box.html") def search_box(request): q = request.GET.get('q','') form = SearchForm({'q': q }) return {'form': form } @register.inclusion_tag('tags/pagination_links.html') def pagination_links(request, paginator): raw_params = request.GET.copy() page = raw_params.get('page',1) p = Paginator(raw_params, 10) try: del raw_params['page'] except KeyError: pass params = urllib.parse.urlencode(raw_params) return {'request': request, 'paginator': paginator, 'p': p, 'params': params } search_box.html <form id="search" action="{% url 'results' %}" method="get"> <h3><label for="id_q">Search</label></h3> <div style="text-align:right;"> {{ form.q }} <input type="submit" value="Search" /> </div> </form> urls.py urlpatterns = [ path('results', views.results, name='results'), ] views.py def results(request): q = request.GET.get('q', '') try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 matching = search.products(q).get('products', []) paginator = Paginator(matching, settings.PRODUCTS_PER_PAGE) try: results = paginator.page(page).object_list except (InvalidPage, EmptyPage): results = paginator.page(1).object_list search.store(request, q) page_title = 'Search Results for: ' + q return render(request, 'search/results.html') results.html {% extends "stk_catalog.html" %} {% load search_tags %} {% block content %} <h1>Search Results for: '{{ q }}'</h1> {% for p in results %} {% include 'tags/product_thumbnail.html' %} … -
Serve single page app under specific path with AWS
At the moment I have mydomain.com pointing to a Django EC2 instance with ABL in front of it and CF in front of the ABL. Now I have the requirement of serving a React single page app under an specific path mydomain.com/my-specific-path (of course everything under HTTPS) I tried everything I could to host my SPA in an S3 bucket and use CF to redirect the calls to that S3. But it was impossible to serve the app over HTTPS that way (because of S3 hosting and subfolders). I am thinking now about setting a reverse proxy in front of my Django app. But I don't know if that is the best solution, and I don't know the best way to do it. Could you please give me some insights about how to serve a SPA under a specific path? Thank you in advance. -
How Can i redirect url to another url with the .htaccess in cpanel
I want to redirect mysite.com/index.php to mysite.com/clientarea.php using .htaccess does not work for me help out. with simple code. I have tried several time and finally the site is not available Try running Windows Network Diagnostics. DNS_PROBE_FINISHED_NXDOMAIN -
Django Serves WebP Images as Octet-Stream
I have a lot of webp images in my static directory. When they loading through img element, everything is alright; but when I'm trying to open one of those webp images in a new tab, they just starting to downloading on my system. I think the problem is in incorrect mime-type Django sets to Content-Type header: application/octet-stream, instead of image/webp. How can I fix it? HTTP/1.1 200 OK Date: Mon, 03 Jun 2019 01:18:08 GMT Server: WSGIServer/0.2 CPython/3.6.8 Content-Type: application/octet-stream Content-Length: 191786 Last-Modified: Thu, 23 May 2019 16:52:23 GMT X-Frame-Options: SAMEORIGIN -
encoding="utf-8" not working when loading data with spanish characters using a command
Don't know why, but a Django Command gont create a bluk of entries after reading a CSV file. The app is hosted on Heroku. I'm using pandas library to read the CSV data, but also using encode('utf-8'). At first it worked, but I had to make some changes to the original file and after saving it and making: python manage.py collectstatic I'm getting error when running the command: python manage.py ubigeo_peru ubigeo_peru.py import pandas as pd import csv from shop.models import Peru from django.core.management.base import BaseCommand tmp_data=pd.read_csv('static/data/ubigeo-peru.csv',sep=',', encoding="utf-8") # tmp_data=pd.read_csv('static/data/ubigeo-peru.csv',sep=',') class Command(BaseCommand): def handle(self, **options): products = [ Peru( departamento=row['departamento'], provincia=row['provincia'], distrito=row['distrito'], costo_despacho_con_recojo=row['costo_despacho_con_recojo'], costo_despacho_sin_recojo=row['costo_despacho_sin_recojo'], dias_despacho = row['dias_despacho'] ) for idx, row in tmp_data.iterrows() ] Peru.objects.bulk_create(products) The data looks good on github and when is opened on excel. https://github.com/OmarGonD/stickers_gallito/blob/master/static/data/ubigeo-peru.csv Error when running the command locally or remotly: $ python manage.py ubigeo_peru D:\virtual_envs\stickers-gallito-app\lib\site-packages\requests\__init__.py:91: RequestsDependencyWarning: urllib3 (1.25.3) or chardet (3.0.4) doesn't match a supported version! RequestsDependencyWarning) Traceback (most recent call last): File "pandas\_libs\parsers.pyx", line 1134, in pandas._libs.parsers.TextReader._convert_tokens File "pandas\_libs\parsers.pyx", line 1240, in pandas._libs.parsers.TextReader._convert_with_dtype File "pandas\_libs\parsers.pyx", line 1256, in pandas._libs.parsers.TextReader._string_convert File "pandas\_libs\parsers.pyx", line 1494, in pandas._libs.parsers._string_box_utf8 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 0: invalid start byte During handling of the above … -
Skip saving row if slug already exists to avoid IntegrityError - Django
I'm setting up a function in my Django Views that calls an API and save the data into my Postgresql database. Everthing was working fine until I got an IntegrityError slugkey already exists, so I'm trying to find a way to skip or ignore the row if the slugify slug already exists. Here's my Django Models: class Product(models.Model): destination = models.CharField(max_length=255, default='') title = models.CharField(max_length=255, default='') slug = models.SlugField(unique=True, max_length=255) description = models.TextField(max_length=2047, default='') link = models.TextField(max_length=500, default='') ptags = TaggableManager() image = models.ImageField(max_length=500, default='images/zero-image-found.png') timestamp = models.DateTimeField(auto_now=True) def _ptags(self): return [t.name for t in self.ptags.all()] def get_absolute_url(self): return reverse('experience', kwargs={'slug': self.slug}) def save(self, *args, **kwargs): if not self.id: self.slug = slugify(self.title) super(Product, self).save(*args, **kwargs) def __str__(self): return self.destination And this is my function in Views: def api_data(request): if request.GET.get('mybtn'): # to improve, == 'something': resp_1 = requests.get( "https://www.headout.com/api/public/v1/product/listing/list-by/city?language=fr&cityCode=ROME&limit=5000&currencyCode=CAD", headers={ "Headout-Auth": HEADOUT_PRODUCTION_API_KEY }) resp_1_data = resp_1.json() base_url_2 = "https://www.headout.com/api/public/v1/product/get/" for item in resp_1_data['items']: print('parsing, translating and saving item {}'.format(item['id'])) # concat ID to the URL string url = '{}{}'.format(base_url_2, item['id'] + '?language=fr') # make the HTTP request resp_2 = requests.get( url, headers={ "Headout-Auth": HEADOUT_PRODUCTION_API_KEY }) resp_2_data = resp_2.json() try: descriptiontxt = resp_2_data['contentListHtml'][0]['html'][0:2040] + ' ...' except (IndexError, KeyError) as e: …