Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Will the local variables be garbage collected when a greenlet was destroyed
My application shows that when the callback func which was sent to a greenlet was returned, the greenlet was finished, but the local variables created by the callback func was still there, not garbage collected by the system. I'm confused by this behavior. Is it true? The backgroud is that I use gunicorn to server my django web application, the worker_class is gevent, so each request was handled by one greenlet. Django application will create a database connection during the request, and stored in threading.local(), the local() was wrapped by gevent, so the connection is in gevent.local(). Due to the django CONN_MAX_AGE settings was not 0, so the connection was not closed when the request finished. when the request was returned, in the theory, the greenlet was finished, and the database connection should be destroyed, but the fact is the connection is still there. -
Django nested prefetch_related
I'm using nested prefetching to link all my objects with each other and then pushing them to a template to show them. My general setup: I have frameworks, categories and controls. A control is linked to a category, a category is linked to a framework. A category can also have a supercategory, in which case the category has a call to another category object. Every control also has an initial score, current score and a target score. What I want to do is for every framework, for every supercategory, for every subcategory show every control with their scores. Example: Framework A Super Category 1 Subcategory 1 Control 1 Initial Current Target Control 2 ... Subcategory 2 Control 3 ... Super Category 2 Subcategory 3 Control 4 ... Framework B ... I've already managed to do this by using a lot of nested prefetches. My models: # models.py #==============[FRAMEWORKS]==============# class Framework(models.Model): name = models.CharField(max_length=100) description = models.TextField(null=True) def __str__(self): return self.name.replace(' ', '_') class FrameworkCat(models.Model): name = models.CharField(max_length=100) identifier = models.CharField(max_length=10, null=True, blank=True, verbose_name="optional identifier") framework = models.ForeignKey(Framework, on_delete=models.CASCADE, verbose_name="related framework") superCat = models.ForeignKey('self', on_delete=models.CASCADE, verbose_name="super category", blank=True, null=True) hexColor = models.CharField(max_length=10, null=True, blank=True, verbose_name="hex color") def __str__(self): return self.name #==============[CONTROLS]==============# β¦ -
bootstrap collapse span is breaking lines when it shouldn't be
I am having a difficulties with a bootstrap collapse button, I am trying to stop a line from breaking after the collapse button. I saw this thread at stackoverflow answer which gives the answer that I should be using instead of to stop the link from breaking but it doesn't work. When I activate the collapse, it changes to the correct format(no line break) for a split second and it goes back to having a line break. Its mysterious because it is correct for a split second, before changing to the incorrect format. The process can be seen on video I have also tried using div class while making changes to style with "style="display: inline" but that doesn't work as well. I trawled through stackoverflow to find a similar question at stackoverflow question but the answer they provided is to wrap a div class=row around my span which does not work at all. I am not sure why the answer to that question made sense since div class=row is for the purpose of breaking a new line. Below is my snippet in html. I am running my app on django. Thanks for any advise anyone can give on this problem β¦ -
'ContentType' object has no attribute 'model_class' in data migration
I have this in my data migration file: def set_target_user(apps, schema_editor): LogEntry = apps.get_model('auditlog', 'LogEntry') ContentType = apps.get_model('contenttypes', 'ContentType') for entry in LogEntry.objects.filter(target_user=None): ct = ContentType.objects.get(id=entry.content_type.id) model = ct.model_class() And I got mentioned AttributeError. But it works well in other modules (not migratins). Any ideas how to overcome this? -
Test django-models decorator
Problem: I need to test my Django Model class decorator. I want temporary model, as I don't want to permanently change existing models or repeatedly decorate models during tests. I understand that I could have emulated model fields and attributes, but my decorator has explicit check that class to be decorated subclasses models.Model. Trying to use custom model did not work: I get relation does not exist error. Question: Is there a way to create temporary models in Django, fake inheritance or not register models? -
How can i add extra model fields to default auth_user model using django
I'm new to Django. I need to include an extra model fields in my User Model (auth_user table in the database) as well as i want to include ForeignKey fields also inside auth_user i have four models classes like Semestertype, Programtype, Programchoice: import datetime from django.utils import timezone from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save from django.contrib.auth.models import AbstractUser class Semestertype(models.Model): value = models.CharField(max_length=100, null=True, blank=True) class Meta: db_table = 'semestertype' class Programtype(models.Model): value = models.CharField(max_length=100, null=True) class Meta: db_table = 'programtype' class Programchoice(models.Model): value = models.CharField(max_length=100, null=True, blank=True) class Meta: db_table = 'programchoice' I included Semestertype , Programtype, Programchoice models in my auth_user models using AbstractUser like below class User(AbstractUser): semestertype = models.ForeignKey(Semestertype, on_delete=models.CASCADE,null=True, blank=True) programtype = models.ForeignKey(Programtype, on_delete=models.CASCADE,null=True, blank=True) programchoice = models.ForeignKey(Programchoice, on_delete=models.CASCADE,null=True, blank=True) filenumber = models.CharField(null=True, max_length=9, blank=True) personalemail = models.EmailField(max_length=254, blank=True) nameofbachelorsdegree = models.CharField(max_length=200, null=True, blank=True) And This is my profile model: class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE,null=True, blank=True) description = models.CharField(max_length=100, default = '') city = models.CharField(max_length=100, default = '') phone = models.IntegerField(default=0) def __str__(self): return self.user.username def create_profile(sender, **kwargs): if kwargs['created']: user_profile = UserProfile.objects.create(user=kwargs['instance']) post_save.connect(create_profile, sender=User) I tried to adding ForeignKey fields and normal fields to auth_user and create β¦ -
Django - User specific transactions
Good morning, We are currently working on a school project where we have to built our own cryptocurrency market. So far so good. A user has a profile page with the following elements: My Wallet, My transactions, My Listings. If "user 1" makes a transaction to "user 2". "User 1" and "2" see this transaction on their respective profile page. However, they see also part of other transactions between other users as well as all the Listings instead of only their own listings. We are completely new to Python and Django but believe that there is a problem in the user assignment in the html tags. Here are our different files that should be relevant: models.py, views.py for the profile page and the html code for the profile page (bootstrap template) <!--This is our custom head title for the different urls. The base.html is loaded after this.--> <head> <title>SolvayCoin - Profile</title> </head> {% extends 'base.html' %} {% block content %} {% if user.is_authenticated %} <!-- Main jumbotron for a primary marketing message or call to action --> <div class="jumbotron"> <div class="container"> <h1 class="display-4">{{ user.first_name }} {{ user.last_name }}'s profile</h1> <p>Username is <pan style="font-style: italic">{{ user.username }}</pan> and joined SolvayCoin {{ β¦ -
Join string with INT django Python
i have my views.py class ShopCreateView(CreateView): fields = ('shop_id',) model = models.Shop def form_valid(self, form): self.object = form.save(commit=False) dept_obj = models.departemen.objects.get(seller_id=self.object.id) dept_name = dept_obj.name date = datetime.datetime.now().date() year = date.year month = date.month seller_count = seller_obj.count() if seller_count > 999: counted = seller_count - 999 self.object.shop_id = dept_name + join_date + year + month + counted self.object.save() return super(ModelFormMixin, self).form_valid(form) here my explain dept_name return string eg. PT. Counter Avenger Manequin = CAM PT. International Job = PIJ I want my code take first char of letter. but don't have idea how to right now. find the solution but its only for show on template.html so self.object.shop_id = dept_name + year + month + counted will like: CAM71804001 its mix string & int. also last 3 har if return 1. i need make it 001 thank you! -
Django CSV file error coercing to Unicode: need string or buffer, ExcelInMemoryUploadedFile found
I am trying to upload a CSV file and then use it. But uploading is giving error. coercing to Unicode: need string or buffer, ExcelInMemoryUploadedFile found Here is my code: csv_data = request.FILES.get('numbulk') with open(csv_data,'rU') as csvfile: spamreader = csv.reader(csvfile,dialect=csv.excel_tab) a = [] for row in csvfile: a.append(row) return HttpResponse(a) I Even tried this: csv_data = request.FILES['numbulk'] with open(csv_data,'rU') as csvfile: spamreader = csv.reader(csvfile,dialect=csv.excel_tab) a = [] for row in csvfile: a.append(row) return HttpResponse(a) -
django leaflet_map how to add geojson layer on demand
I have a geodjango project using django-leaflet. A base map is displayed in the body of the html file using {% leaflet_map "mapid" %} . No other initialization is needed for the base map to display. Also, there is a choice box of polygon features that user can select. Upon selection the selected feature should display on top of the base map. As shown, the selected feature is the success response of a post ajax call. I get the correct data, however, I don't know how to display it on the map. <html> <head> {% load leaflet_tags %} {% leaflet_css %} {% leaflet_js %} <style> #mapid { width: 870px; height: 530px; } </style> </head> <body> <div> {% leaflet_map "mapid" %} </div> <form id="aoi-form" method="post" action="" > {% csrf_token %} <select id="aoi-choice" name="modelarea_sel_name" > {% for aoi in modelarea.aois.field.choices %} <option>{{aoi|title}}</option> {% endfor %} </select> </form> <script type="text/javascript"> $(document).ready(function() { // sets the csrf token for all the ajax calls setCSRFToken(); function displayModelArea(data) { window.addEventListener("map:init", function(event) { var map = event.detail.map; L.geoJson(data, { onEachFeature: function onEachFeature(feature, layer) { layer.bindPopup(feature.properties.name); } }).addTo(map); }); } $('#aoi-choice').on('change', function(e) { var selecteditem = $(this).find("option:selected").val(); $.ajax({ url: $("#aoi-form").attr("action"), type: $("#aoi-form").attr("method"), data: { "aoi-choice": selecteditem }, success: β¦ -
Django,page not found 404
When I start developement server Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/ Then Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: ^admin/ ^blog/ The empty path didn't match any of these. mysite.urls.py urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^blog/', include('blog.urls')), ] I suspect something is wrong with my templates This is what tree gives tree mysite mysite βββ blog β βββ admin.py β βββ apps.py β βββ __init__.py β βββ migrations β β βββ 0001_initial.py β β βββ __init__.py β β βββ __pycache__ β β βββ 0001_initial.cpython-36.pyc β β βββ __init__.cpython-36.pyc β βββ models.py β βββ __pycache__ β β βββ admin.cpython-36.pyc β β βββ __init__.cpython-36.pyc β β βββ models.cpython-36.pyc β β βββ urls.cpython-36.pyc β β βββ views.cpython-36.pyc β βββ templates β β βββ blog β β βββ base.html β β βββ post β β βββ detail.html β β βββ list.html β βββ tests.py β βββ urls.py β βββ views.py βββ db.sqlite3 βββ manage.py βββ mysite βββ __init__.py βββ __pycache__ β βββ __init__.cpython-36.pyc β βββ settings.cpython-36.pyc β βββ urls.cpython-36.pyc β βββ wsgi.cpython-36.pyc βββ settings.py βββ urls.py βββ wsgi.py -
Django authenticate method fails during tests
I have custom User model called Customer. I'm working on token based authentication with django-rest-framework and writing tests. test_get_auth_token fails in serializer when I try to call authenticate function from django.contrib.auth, so I get my message "Unable to log in with provided credentials." I guess this problem is related to password hashing, but I can't solve it. I also tried to create user with self.user = Customer.objects.create(email="asd@asd.com") self.user.set_password("qwerty123") self.user.save() but it also not worked. test.py def setUp(self): self.user = Customer.objects.create_user('test@example.com', '12345678') self.get_token_url = reverse('customers:get-auth-token') def test_get_auth_token(self): data = { 'email': 'test@example.com', 'password': '12345678' } response = self.client.post(self.get_token_url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) views.py class CustomerAuthToken(ObtainAuthToken): def post(self, request): serializer = AuthCustomerSerializer(data=request.data) if serializer.is_valid(): token, created = Token.objects.get_or_create( user=serializer.validated_data['user']) if not created: # update the created time of the token to keep it valid token.created = timezone.now() token.save() json = serializer.data json['token'] = token.key return Response(json, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializers.py class CustomerAuthSerializer(serializers.ModelSerializer): email = serializers.CharField() password = serializers.CharField(min_length=8, write_only=True) def validate(self, attrs): email = attrs.get('email') password = attrs.get('password') user = None if email and password: if validate_email(email): user = authenticate(email=email, password=password) if user: if not user.is_active: msg = 'User account is disabled.' raise exceptions.ValidationError(msg) else: msg = 'Unable to log in β¦ -
Adding a field from a formset to add_error
I Have following code in admin.py: from django.contrib import admin from .models import Timesheet from .models import Action import requests class ActionAdmin(admin.TabularInline): model = Action extra = 0 class TimesheetAdmin(admin.ModelAdmin): inlines = [ActionAdmin] def save_formset(self, request, form, formset, change): instances = formset.save(commit=False) for instance in instances: url = 'https://xxx.xxxxxx.com/api.php' contents = requests.get(url) if contents.status_code == 200 and contents.json()['result'] == 'success': instance.ticket_title = contents.json()['subject'] instance.save() else: form.add_error('?????', 'Ticket ID Not Found') formset.save_m2m() admin.site.register(Timesheet, TimesheetAdmin) I have ticket_id field in formset. What should I pass to the add_error for indicate this field? -
python manage.py runserver 0:8888 is get error -: Error: [Errno 11001] getaddrinfo failed
show this error when i'm running python given below command get this error manage.py runserver 0:8888 -
TInder swipe cards feature from scratch?
I want to add the tinder swipe right-left feature in the application. The feature will be like, see the photos (cards) swipe right or left and store the results of the user in the database. So I want some tips how can I build this feature from scratch. Any suggestions, general workflow, tools, and resources I can use to build this. Our application is Django based application. Thank you in Advance -
How can i make search fields for seperate individula columns for django admin page?
I want to make similar django admin page where each columns of the tabel has its individual search field -
Speed up "Find points in polygon"-query in geodjango
I am working on a geodjango application where I want to find all the geogrpahic locations in a polygon. The idea is that I store all geographic information (cities, countries, POI, etc.) in a table and if I want to find everything inside a (multi)polygon that I query the database to find those points. I have the following models: class Location(models.Model): name = models.CharField(max_length=250, unique=True) geometry = models.GeometryField(null=True, blank=True) class Project(models.Model): name = models.CharField(max_length=2000, blank=False, null=False, unique=True) location = models.ManyToManyField(Location, related_name='projects', blank=True) In my view I have: class LocationDetailView(DetailView): model = Location def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) location_geometry = self.get_object().geometry location_projects = [] if 'Polygon' in location_geometry.geom_type: # location is a Polygon or MultiPolygon for loc in Location.objects.prefetch_related('projects').all(): if location_geometry.contains(loc.geometry): for location_project in loc.projects.all(): location_projects.append({'name': location_project.name, 'pk': location_project.pk}) # Add in a QuerySet of all the projects context['projects'] = location_projects return context This is quite slow (and I do not have only a few hundred locations in the DB). As you can see I've tried speeding this up by prefetching the projects, but when I look at the SQL in the debug toolbar I see that (although it is a fast query) one query is duplicated 259 times: SELECT β¦ -
What does P mean in `/(?P<topic_id>\d+)$`
This might be frivolous, I donβt know the origin of P in /(?P<topic_id>\d+)$", Google searches arenβt helpful. Python official docs does not elaborate it on 6.2. re [ # page for adding a new new Entry url(r"^new_entry/(?P<topic_id>\d+)$", views.new_entry, name='new_entry'), ] Does the βPβ mean βpatternβ which seems unnecessary to declare it. -
How to mutate validators of fields in model serializer
I use a ModelSerializer because it automatically add serializer fields. class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('id', 'last_login', 'email', 'is_admin', 'password_expiry', 'is_active', 'created_timestamp', 'modified_timestamp') It generates serializer like below. UserSerializer(): id = IntegerField(label='ID', read_only=True) last_login = DateTimeField(allow_null=True, required=False) email = EmailField(label='Email address', max_length=255, validators=[<UniqueValidator(queryset=User.objects.all())>]) is_admin = BooleanField(required=False) password_expiry = DateTimeField(allow_null=True, required=False) is_active = BooleanField(required=False) created_timestamp = DateTimeField(read_only=True) modified_timestamp = DateTimeField(read_only=True) In that we can see that it automatically add a UniqueValidator to email filed. So, If I do serializer.is_valid() it will perform field type validation for all fields and additionally UniqueValidator for email field. That leads to a problem. I have an API to find list of users by email id. For simplicity I used the existing serializer to dynamically create custom serializer. CustomUserSerializer = UserSerializer CustomUserSerialize.Meta.fields = ('email',) Now, what I expect to perform is to validate only the email type but not UniqueValidator. Please note that I don't wan't to have a separate serializer for this in my source code. -
How to use non default database of django in pandas.read_sql_query?
I saw this article which says SQL Optimisation with Django and Pandas here is the link of the article. This article is fine but in my project i am using two database and i want to use non default database.Here is my code:- def to_df(queryset): try: query, params = queryset.query.sql_with_params() except EmptyResultSet: # Occurs when Django tries to create an expression for a # query which will certainly be empty # e.g. Book.objects.filter(author__in=[]) return pd.DataFrame() print query print params print pd.io.sql.read_sql_query(query, connection, params=params) return pd.io.sql.read_sql_query(query, connection, params=params) @profile() def demo(request): to_df(Users.objects.using('cms').all()) Things i tried to overwrite connection are:- print pd.io.sql.read_sql_query(query, connection.using('cms'), params=params) conection.connect(host='',username='root',password='root') but it didnt work.While cms is the name of my non default database which is deifined in swttings.py file. -
How can I convert this SQL statement to Django QuerySet?
PLEASE NOTE: This question is a follow-up to this question I asked a few days ago. It is not a duplicate. There are slight but significant differences in the SQL Query I'm trying to model in Django and the dummy data I have loaded. I'm writing a Python/Django application to do some stock analysis. I have two very simple models that look like this: class Stock(models.Model): symbol = models.CharField(db_index=True, max_length=5, null=False, editable=False, unique=True) class StockHistory(models.Model): stock = models.ForeignKey(Stock, related_name='StockHistory_stock', editable=False) trading_date = models.DateField(db_index=True, null=False, editable=False) close = models.DecimalField(max_digits=12, db_index=True, decimal_places=5, null=False, editable=False) class Meta: unique_together = ('stock', 'trading_date') This is the dummy data I have populated them with: import datetime a = Stock.objects.create(symbol='A') b = Stock.objects.create(symbol='B') c = Stock.objects.create(symbol='C') d = Stock.objects.create(symbol='D') StockHistory.objects.create(trading_date=datetime.date(2018,1,1), close=200, stock=a) StockHistory.objects.create(trading_date=datetime.date(2018,1,2), close=150, stock=a) StockHistory.objects.create(trading_date=datetime.date(2018,1,3), close=120, stock=a) StockHistory.objects.create(trading_date=datetime.date(2018,4,28), close=105, stock=a) StockHistory.objects.create(trading_date=datetime.date(2018,5,2), close=105, stock=a) StockHistory.objects.create(trading_date=datetime.date(2018,5,3), close=105, stock=a) StockHistory.objects.create(trading_date=datetime.date(2017,5,2), close=400, stock=b) StockHistory.objects.create(trading_date=datetime.date(2017,11,11), close=200, stock=b) StockHistory.objects.create(trading_date=datetime.date(2017,11,12), close=300, stock=b) StockHistory.objects.create(trading_date=datetime.date(2017,11,13), close=400, stock=b) StockHistory.objects.create(trading_date=datetime.date(2017,11,14), close=500, stock=b) StockHistory.objects.create(trading_date=datetime.date(2018,4,28), close=105, stock=c) StockHistory.objects.create(trading_date=datetime.date(2018,4,29), close=106, stock=c) StockHistory.objects.create(trading_date=datetime.date(2018,4,30), close=107, stock=c) StockHistory.objects.create(trading_date=datetime.date(2018,5,1), close=108, stock=c) StockHistory.objects.create(trading_date=datetime.date(2018,5,2), close=109, stock=c) StockHistory.objects.create(trading_date=datetime.date(2018,5,3), close=110, stock=c) StockHistory.objects.create(trading_date=datetime.date(2018,5,4), close=90, stock=c) I want to find all the stocks that made a yearly low within the past week. But to make this question simpler, just assume β¦ -
Modify django HTTP terminal console logs
I am new to python. So I need to modify the logs implicitly generated when I make an API call like for example [2018/05/11 11:28:06] HTTP POST /admin/api/accounts-details/ 200 [0.04, 127.0.0.1:48798] . So I dont know from where this line is getting printed and want to modify it to also include the user whose making the request. I made changes to 'LOGGING' entry in settings.py too. Any help will be appreciated. -
Django: using a premade mysql db with no relationships defined
I have a mysql db with data in it and no relationships defined. What I want is to use this database in a Django project and I want it to have the relationships defined. There are fields that are supposed to be ForeignKeys but are defined as IntegerFields. So basically I want to change them to foreignKeys. What I have: user_id = models.IntegerField(blank=True, null=True) What I Want: user_id = models.ForeignKey('User', on_delete=models.CASCADE) -
ERROR: test_board_topics_view_not_found_status_code - self.model._meta.object_name DoesNotExist: Board matching query does not exist
i am a beginner in python especially django. I followed the practice at simpleisbetterthancomplex.com. but found a problem while testing. $ python manage.py test and my results System check identified 1 issue (0 silenced). ...E............ ====================================================================== ERROR: test_board_topics_view_not_found_status_code (boards.tests.BoardTopicsTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/sysadmin/Documents/belajar_python/Django/mysite/boards/tests.py", line 41, in test_board_topics_view_not_found_status_code response = self.client.get(url) .... .... .... File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 380, in get self.model._meta.object_name DoesNotExist: Board matching query does not exist. and my code @ https://github.com/githubfans/myfirstdjango/tree/master/mysite my version $ python manage.py shell Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> import django >>> django.VERSION (1, 11, 12, u'final', 0) is there anybody who can enlighten me? -
How to solve the error ImportError: cannot import name 'AsyncWebsocketConsumer'?
I am using Python 3.6 ,Django 1.11 and chennels=2. I am following this tutorial and it works fine till tutorial part 2: http://channels.readthedocs.io/en/stable/tutorial/part_2.html When i moved to tutorial part 3 and changed ChatConsumer file accordingly: http://channels.readthedocs.io/en/stable/tutorial/part_3.html I am facing this issue ImportError: cannot import name 'AsyncWebsocketConsumer' Any kind of will be appreciated!