Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Group_by in Django Database | Most efficient query order
I'm working on an old django app which unfortunately has one giant table in the database, meaning conventionally our queries are very slow. We cannot migrate the DB nor re-factor it into multiple tables for reasons beyond my control. I would like to make sure that these queries are as efficient as possible because they are a bit more complex than the queries that are typically being done in this app. For the following questions consider the following table schema: Table Orders: name: string price: integer order_date: date Django version = 1.6.5 Question 1 I'm coming from Rails, in which I would typically use the groupdate gem to group output as such: >> Orders.group_by_day() >> >> { 2013-1-1: {<all orders taking place on Jan 1st, 2013>}, 2013-1-2: {<Jan 2nd 2013>}, ... } Is there something built into Django that does something similar efficiently? I found https://docs.djangoproject.com/en/2.0/topics/db/aggregation/ but it doesn't appear as though it outputs in a similar manner. Question 2 Does the order in which you stack queries affect the speed at which it is executed? For example given the same query written in 3 different ways: Orders.where(price > 5).group_by_day() Orders.group_by_day().where(price > 5) { partial_orders = Orders.where(price > 5) partial_orders … -
using request.user in templatetags in Django
I am using Django 2.0 I have three tables notes, starred and shared notes model def Notes(models.Model): title = models.CharField() def Starred(models.Model): note = models.ForeignKey(Note, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE) def Shared(models.Model): note = models.ForeignKey(Note, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE) the note can be shared with multiple users and multiple users can star the note which will be listed in their /starred notes views.py has class SharedNotes(ListView): template_name = 'notes/shared.html' model = Shared context_object_name = 'shared_notes' def get_queryset(self): shared_notes = Shared.objects.filter(user=self.request.user) return shared_notes @method_decorator(login_required) def dispatch(self, request, *args, **kwargs): return super(self.__class__, self).dispatch(request, *args, **kwargs) I have written a separate template snippets to display list of notes and add * (star) if it is starred by logged in user using {% if note.starred_set.all %} <span class="card-star"> <i class="material-icons">star</i> </span> {% endif %} But this way star is showing with other users the note has been shared with even when they have not starred the note but has been starred by someone else. How to filter the starred only to logged in user? I thought of to write a tag to be used like {% if star.pk|starred %} <!-- add html star code here --> {% endif %} and filter tag in … -
Returning distinct objects sorted by SearchRank involving multiple tables
I have a model class called Poll which has title, description, and tags fields. I also have a PollEntry class (related_name=entries) which also has title, description, and tags. I am trying to implement text search (using contrib.postgres.search) module. There are apparently issues with returning a search ordered by rank returning duplicate objects and the Django documentation says basically "You have to be careful", but does not give any examples of how to deal with this, and I have had basically no luck finding examples online (on SO or elsewhere). The following code snippet appears to solve this problem, but I don't know if it is the most efficient way to do this. Any suggestions or references would be much appreciated! Also, note I am using DRF here. @list_route(methods=['get']) def search(self, request): search_query_terms = request.query_params.get('searchQuery').split(' ') search_vector = SearchVector('entries__tags__name')+\ SearchVector('title')+\ SearchVector('description')+\ SearchVector('tags__name')+\ SearchVector('entries__title')+\ SearchVector('entries__description') search_query = SearchQuery(search_query_terms[0]) for term in search_query_terms[1:]: search_query = SearchQuery(term) | search_query ids = Poll.objects\ .annotate(rank=SearchRank(search_vector, search_query))\ .order_by('-rank')\ .filter(rank__gt=0)\ .values_list('id') polls = Poll.objects.filter(id__in=ids) serializer = self.get_serializer(polls, many=True) return Response( data=serializer.data ) -
How to increment a numeric string in Python
I've spent the last two days trying to figure out how to increment a numeric string in Python. I am trying to increment a sequence number when a record is created. I spent all day yesterday trying to do this as an Integer, and it works fine, but I could never get database to store leading zeros. I did extensive research on this topic in StackOverflow, and while there are several examples of how to do this as an Integer and store leading zeros, none of the examples worked for me. Many of the examples were from 2014, so perhaps the methodology has changed. I then switched over to a String and changed my attribute to a CharField, and can get the function to work with leading zeros, but now I can't seem to get it to increment. Again, the examples that I found on SO were from 2014, so maybe things have changed a bit. Here is the function that works, but every time I call it, it doesn't increment. It just returns 00000001. I'm sure it's something simple I'm not doing, but I'm out of ideas. Thanks in advance for your help. Here is the function that works … -
ObtainAuthToken fails with 404 when using local network IP
I'm trying to connect an Android device to my ObtainAuthToken endpoint from the django-rest-framework. If I just call it with http://localhost/api-token-auth it works fine: [21/Dec/2017 16:04:34] "POST /api-token-auth HTTP/1.1" 400 68 But if I change that to http://192.168.1.4/api-token-auth my dev server throws me a 404, like so: [21/Dec/2017 16:05:00] "POST /api-token-auth HTTP/1.1" 404 0 I've tried adding my IP number 192.168.1.4 to ALLOWED_HOSTS but no dice. Trying to Google this issue just brings up a bunch of unrelated stuff. Anyone knows what might be causing this? -
SendNotification parameters dosen't work in Google API calendar
I'm using Google Calendar API library with django. creating or updating an event with the request parameter "sendNotification" set to True, the guests included in the event do not receive any notification via email. This is the code : created_event = service.events().update(calendarId=calendar_id,sendNotifications=True, eventId=event_id, body=modified_event).execute() the Notification setting in the calendars are correct ( enabled , and Email type is selected ) I'm using a service account for authentication, I do not know if it can affect thanks for the support -
Django query with annotate and select related together
I'm unsuccessfully trying to google the solution for this simple case: There are Book and Author models. I can have several same books (with different IDs). Author is a foreign key within Book. I need simple way how to list all books, their count and author name. I can do: Book.objects.values("book_name","author").annotate(count=Count("book_name") which gives me an array where aditional value with count of "book_name" items. But instead of author name it returns just Author object id. I can get author name using Book.objects.select_related() Which replaces the author's ID with related object so I can get the author.name but it doesn't work with .values() Any idea how to get this working together? so I can have both count of each Book and author name? I'd like to avoid complex solution like querying Books, and within the loop construction of a result array and querying Authors name for each book. Thanks a lot. TC -
How can I add an Upload File field to a Wagtail Form?
I want to make File Upload a possible Wagtail Form field type on form pages. How do I configure the models to make this possible? -
django.db.utils.OperationalError: (1054, "Unknown column 'zoneclient_user.last_sync_time' in 'field list'")
If I run makemigrations, it is fine. However, if I decide to run migrate in bash, then I got the following traceback. I'll show you below the model associated Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv super(Command, self).run_from_argv(argv) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **options.__dict__) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute super(Command, self).execute(*args, **options) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute output = self.handle(*args, **options) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle failures = test_runner.run_tests(test_labels) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/test/runner.py", line 147, in run_tests old_config = self.setup_databases() File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/test/runner.py", line 109, in setup_databases return setup_databases(self.verbosity, self.interactive, **kwargs) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/test/runner.py", line 299, in setup_databases serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True), File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/backends/creation.py", line 377, in create_test_db test_flush=True, File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command return klass.execute(*args, **defaults) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute output = self.handle(*args, **options) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 165, in handle emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/sql.py", line 276, in emit_post_migrate_signal db=db) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 198, in send response = receiver(signal=self, sender=sender, **named) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/guardian/management/__init__.py", line 31, in create_anonymous_user User.objects.get(pk=guardian_settings.ANONYMOUS_USER_ID) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/models/query.py", … -
Object of type JSON non serializable django REST Framwork
Hi guys I am having the error Object of type 'MyUser' is not JSON serializable, I checke on many post that has the same issue but nothing seems to make my code work Could someone help me please ? in my views: class TeamChartData(APIView): queryset = MyUser.objects.all() serializer_class = MyUserSerializer permission_classes = [] http_method_names = ['get',] def get_serializer_class(self): return self.serializer_class def get(self, request, format=None, *args, **kwargs): chunk_team = get_chunk_team(self) data = { "chunk_team":chunk_team } return Response(data) serializer.py : from rest_framework.serializers import ModelSerializer from rest_framework import serializers from registration.models import MyUser from website.models import Team,Project from survey.models import Response class MyUserSerializer(ModelSerializer): team = serializers.SerializerMethodField() class Meta: model = MyUser fields = ( 'email', 'first_name', 'last_name', 'team', ) def get_team(self, obj): #print(obj) # for you to understand what's happening teams = Team.objects.filter(members=obj) serialized_teams = TeamSerializer(teams,many=True) return serialized_teams.data class TeamSerializer(ModelSerializer): class Meta: model = Team fields = ( 'team_name', 'team_hr_admin', 'members', ) -
django 1.11 raise forms.validationError not showing the error in html
I've tried looking around for solutions on how to check if a form's name is already existing in the database. I used this link to figure out how, and it is indeed not allowing duplicate names to be entered. But where I expected one, I did not get an error message. I'm not sure what I'm doing wrong here, so if anyone can tell me what I should do, that would be really useful! addgame.html: <form method="POST" class="post-form" enctype="multipart/form-data"> {% csrf_token %} {% if form.non_field_errors %} {% for error in form.non_field_errors %} {{ error }} {% endfor %} {% endif %} <div class="form-group"> {{ form.name.label_tag }} {% render_field form.name class="form-control" %} <br> {{ form.genre.label_tag }} {% render_field form.genre class="form-control" %} <br> {{ form.image.label_tag }} {{ form.image }} </div> <hr> <button type="submit" class="save btn btn-primary">Save</button> </form> views.py: def addgame(request): if request.method == "POST": form = InfoForm(request.POST, request.FILES) if form.is_valid(): infolist = form.save(commit=False) infolist.created_date = timezone.now() infolist.save() return redirect('index') else: form = InfoForm() return render(request, 'ranking/addgame.html', {'form': form}) forms.py: class InfoForm(forms.ModelForm): class Meta: model = GameInfo fields = ('name', 'image', 'genre') def clean_name(self): name = self.cleaned_data['name'] try: match = GameInfo.objects.get(name=name) except GameInfo.DoesNotExist: return name raise forms.ValidationError('This game has already been added to … -
How to redirect url with added url parameters
I have a workout calendar app where if a user goes to /workoutcal/, I want them to be redirected to workoutcal/<today's year>/<today's month> so that this route will send their request to the correct view: url(r'^(?P<year>[0-9]+)/(?P<month>[1-9]|1[0-2])$', views.calendar, name='calendar'), So how can I write a new url pattern in urls.py that does something like: url(r'^$', RedirectView().as_view(url=reverse_lazy(),todays_year, todays_month)), -
How to get the type of a class property in python
I have a Python class with different attributes but I do not seem to find how to get the type of each of them. I want to check whether if a given attribute has an specific type. Be aware that I am talking about the class and not an instance. Let's say that I have this class: class SvnProject(models.Model): ''' SVN Projects model ''' shortname = models.CharField( verbose_name='Repository name', help_text='An unique ID for the SVN repository.', max_length=256, primary_key=True, error_messages={'unique':'Repository with this name already exists.'}, ) How can I check whether shortname is a model.CharField or a model.Whatever? -
Django admin disable foreign key drop down but keep add button next to it
I want the dropdown for particluar foreign key to be disabled,but want the "plus" button intact.so that in admin a user can only add the value but cannot edit or select from the list. -
Celery Database Backend Missing connection string! Do you have the database_url setting set to a real value?
After upgrading to Celery 4.1.0 I got this error. While running my Django app. Missing connection string! Do you have the database_url setting set to a real value? -
Python Dictionary Update method not doing as I expect
I bumped into this method while trying to see how django BaseContext works. def flatten(self): """ Return self.dicts as one dictionary. """ flat = {} for d in self.dicts: flat.update(d) return flat When I try to recreate the method on my own like this A = { 'c':1,'d':2,'a':3,'b':4 } B = { 'b':2,'a':1,'d':4,'c':3 } flat = {} for x in A: flat.update(x) print(flat) I am getting a ValueError: dictionary update sequence element #0 has length 1; 2 is required From the dictionary update documentation it says the method accepts either another dictionary object or an iterable of key/value pairs. What am I not getting right since I think I am passing in a dictionary to the method?? -
PDFkit - different page size on production machine
I have a Django project in which I generate PDF from HTML with PDFkit. On my local machine I see the pdf just fine but on production machine the generated pdf page is a little bigger. The html is correct and identical on both machines. The OSes are the same (ubuntu 14.04). Checked all the packages versions and they are the same. My pdf generating code is: options = {'page-size': 'A2', 'margin-top': '0in', 'margin-right': '0in', 'margin-bottom': '0in', 'margin-left': '0in'} pdf_location = '/...../executive_summary.pdf' pdfkit.from_string(my_template.render(context_dict), pdf_location, options=options, css='/....../executive_summary.css') wkhtmltopdf is the latest pdfkit==0.6.1 The screenshots are attached. Where else can be the problem? -
virtualenvwrapper - environment variables are not saving
I have a problem with virtualenvwrapper on ubuntu. I'm working on simple Django project foo. I've decided to move SECRET_KEY from settings.py and save it as environment variable. Everything went well: In bash I've entered export SECRET_KET=['...'] In settings.py I've entered: SECRET_KEY = os.environ['SECRET_KEY'] Also I tested that app is working and everything was ok. BUT After I've started working on project once again using workon foo command in bash and I've tried to simply run server with python manage.py runserver and the SECRET_KEY is not working. The error I get is: ... SECRET_KEY = os.environ['SECRET_KEY'] File "/home/user/.virtualenvs/foo/lib/python3.6/os.py", line 669, in __getitem__ raise KeyError(key) from None KeyError: 'SECRET_KEY' I've read that this variable should be set in postactivate file in .virtualenvs/foo/bin/, but there is nothing there. Fortunately it was just simple test project, but how can I be sure that environment variable will be saved in my next virtualenv when using virtualenvwrapper? -
Django custom manager`s method that changes data works incorrectly with other maager`s methods (filter, get, etc.)
I have created a custom Manager, but in my case its method adds some string to the end of particular field in a model instead of just filtering queryset as in usual cases. My goal is to return already changed objects when calling SomeModel.objects. Django`s documentation says: You can override a Manager’s base QuerySet by overriding the Manager.get_queryset() method. get_queryset() should return a QuerySet with the properties you require. My approach works, when I call SomeModel.objects.all(), but if I apply some filter for objects or just after .all() I can see that data become just regular. models.py: class BaseModelQuerySet(models.QuerySet): def edit_desc(self, string): if self.exists(): for obj in self: if 'description' in obj.__dict__: obj.__dict__['description'] += string return self class BaseModelManager(models.Manager): def get_queryset(self): return BaseModelQuerySet(self.model, using=self._db).edit_desc('...extra text') class BaseModel(models.Model): objects = BaseModelManager() class Meta: abstract = True Shell output: >>> Macro.objects.all()[0].description 'Test text...extra text' >>> Macro.objects.all().filter(id=1)[0].description 'Test text' That makes me confused. Such impression that other methods calling regular queryset instead of one returned with custom objects. I`m looking forward for any help and thanks in advance! -
Python:django: TypeError: __init__() missing 1 required positional argument: 'on_delete'
from django.db import models class Topic(models.Model): text = models.CharField(max_length=200) date_added = models.DateTimeField(auto_now_add=True) def _str_(self): return self.text class Entry(models.Model): topic = models.ForeignKey(Topic) text = models.TextField date_added = models.DateTimeField(auto_now_add=True) class Meta: verbose_name_plural= 'entries' def _str_(self): return self.text[:50]+"..." This is my code,When i run: python3 manage.py makemigrations learning_logs The answer is : TypeError: __init__() missing 1 required positional argument: 'on_delete' WHY THIS WARNING ? I read the documentation. But this problem doesn't sovled . -
Cannot assign "(<VlanedIPv4Network: 103.35.203.6/31>,)": "IPv4Manage.vlanedipv4network" must be a "VlanedIPv4Network" instance
I have a IPv4Manage model: class IPv4Manage(models.Model): ... vlanedipv4network = models.ForeignKey( to=VlanedIPv4Network, related_name="ipv4s", on_delete=models.DO_NOTHING, null=True) And when I update the ipv4 instance: vlaned_ipv4_network = VlanedIPv4Network.objects.create( network_ip = ip_network_divided_obj.network_address.exploded, prefix = ip_network_divided_obj.prefixlen, gateway_ip = gateway_ip.exploded, broadcast_ip = ip_network_divided_obj.broadcast_address.exploded, ipv4networkmanage = vlanable_ipv4network.ipv4networkmanage, ) ... ipv4 = IPv4Manage.objects.get(ip=ip) ipv4.netmask = bits_to_mask(ip_network_divided_obj.prefixlen) if ipv4.ip == gateway_ip: ipv4.is_gateway = True else: ipv4.is_gateway = False ipv4.ip_status = IPV4_STATUS.已Vlan化未配置服务器 ipv4.vlanedipv4network = vlaned_ipv4_network, # there comes the issue ipv4.save() Cannot assign "(<VlanedIPv4Network: 103.35.203.6/31>,)": "IPv4Manage.vlanedipv4network" must be a "VlanedIPv4Network" instance. But my vlaned_ipv4_network exactly is VlanedIPv4Network instance. -
Django regex in url patterns
Is there any difference to use [0-9]+ vs d+ in django url patterns? Any security difference? -
Django/Python register page
I'm making a register page for my django website, but it pretty messy to me, I'm pretty sure the bullet points and the additional information is not supposed to show up right away. How can I make this register page look cleaner? Ie. just the username, password, and confirmation textbox, rather than all the messages. Thanks! Register.html <h2>Sign up</h2> <br> <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Sign up</button> </form> Register view def register(request): if request.method == 'POST': form = UserCreationForm(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('index') else: form = UserCreationForm() return render(request, 'todo/register.html', {'form': form}) -
Build m2m relationship for parents to children and siblings
To describe my problem let's assume there is a model class Person: class Person(models.Model): name = models.CharField(max_length=100, unique=True) sex = models.CharField( max_length=1, choices=( ('F', 'female'), ('M', 'male') ) ) relatives = models.ManyToManyField('self', blank=True) def __str__(self): return self.name A person can have many (or none) relatives of these types: parents children siblings halfsiblings (maternal or paternal) In order to describe the relationship type we need another model class and the option through, which in this case will require also the option symmetrical=False. The relation parent-child is indeed asymmetrical, however the siblings relation is symmetrical. I'm looking for an elegant and clear solution to overcome the dilemma between the true symmetrical relation of siblings and the asymmetrical relation of parents and children. Adapting the field relatives and adding additional model class results in: class Person(models.Model): # ... relatives = models.ManyToManyField( 'self', through='Relatedness', symmetrical=False, blank=True ) class Relatedness(models.Model): RELATIONSHIPS = ( ('PA', 'parent'), ('SI', 'sibling'), ('MH', 'maternal halfsibling'), ('PH', 'paternal halfsibling'), ) source = models.ForeignKey('Person', related_name='source') target = models.ForeignKey('Person', related_name='target') relationship = models.CharField(max_length=2, choices=RELATIONSHIPS) class Meta: unique_together = ('source', 'target') What I need is: to show all relatives for a person to implement constraints that there are no weird combinations like if A … -
How to verify auth key of another API in my Api?
I have made an Django API(1), which calls another API(2) for data. Problem I am facing is that other API(2) use HMAC auth, so when logged in that API(2), I am getting the result but using the same app(1) in other browser { "message": "Unauthorized", "success": false } this message is coming. I know I need to put the auth key of API(2) somewhere in my views.py(1) file and currently I am doing this, def search(request): auth = HmacAuth('Username', 'mySEcretKey') response = requests.get('https://api(2).com/api/get_nearest', auth=auth, params=dict(token='imatinib')) print json.dumps(response.json(), indent=4) which is obviously not working.