Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Purpose of __init__.py. Or: Why put code in __init__.py instead of a module?
I am reviewing Django's code. It has these packages: β django pwd /Users/ugur/.virtualenvs/evernote/lib/python3.6/site-packages/django β django tree -L 1 . βββ __init__.py βββ __main__.py βββ __pycache__ βββ apps βββ bin βββ conf βββ contrib βββ core βββ db βββ dispatch βββ forms βββ http βββ middleware βββ shortcuts.py βββ template βββ templatetags βββ test βββ urls βββ utils βββ views Now I dived into the package in db/ and try to understand the structure. Classes that I use very often when I create my models are for instance: models.CharField models.TextField models.IntegerField In trying to review their code, I was surprised to find them in: db/models/fields/__init__.py Looking into that __init__.py file, I find >2300 lines of code. I was using __init__.py files to indicate to the interpreter that a folder is meant to be a package consisting of modules. Also, checking attributes of an object via dir(myobject) I can see that the __file__ attribute points to __init__.py file. For example dir(django): In [5]: dir(django) Out[5]: ['VERSION', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'get_version', 'setup', 'utils'] ## so `django.__file__` equals to ./django/__init__.py` ## In [6]: django.__file__ Out[6]: '/Users/ugur/.virtualenvs/evernote/lib/python3.6/site-packages/django/__init_ _.py' In [7]: django.__package__ Out[7]: 'django' In [8]: What (else) is β¦ -
Django validating a (newsletter) form included as part of the base template
I have a simple newsletter sign form which I want to include on every page of the site. Here is the form (subscribers/forms.py) from django import forms class SubscriberForm(forms.Form): email = forms.EmailField() first_name = forms.CharField(max_length=20) last_name = forms.CharField(max_length=30) I have set up a context processor, so I can render the form in the base template eg def subscriber_form(request): return { 'subscriber_form':SubscriberForm(), } And in the base template I have {% include "subscribers/index.html" with form=subscriber_form %} All good. However, the question is how do I validate the form without taking the user to a different url (and view). I obviously don't want to take the user to (eg) /subscriber/index (hence the action would be "'subscriber/index") and the associated view where I validate the form, because I want the validation errors (if any) to be shown on the (base) template. I would also want the success message to be shown on the base template, rather than taking the user off to a "success" url. It's also not a problem to do the validation using JavaScript but I want to have a fallback in case JS is disabled. This is all because I want the form to be displayed on every single page β¦ -
Django - Get objects that don't have one to to one attribute
I have a concept where there is a model that has a OneToOne field with User. How can I query for all users that are not assigned to one of these? For example: class SpecialUser(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) Not all users are special users. How can I get all users that are not special users using objects (ie: User.objects.filter(something)). Thanks! -
Why does the date/time field in my handsontable not update my database?
I am working on a web application built in Django. I am using a handsontable in the browser to interact with the database. I have a field set up for date/time that is not updating the database. It seems to be a data type issue but I cannot figure out why. In models.py I have the following defined: sampled = models.DateTimeField(blank=True, null=True) And in my html file I am building the handsontable in the javascript with the following settings for this field: data: 'sampled', type: 'date', dateFormat: 'DD/MM/YYYY HH:mm', Any suggestions? -
Difference with previous object in django queryset annotation
Let's assume that I have following model: class TestModel(models.Model): some_integer = models.IntegerField() and I have 3 instances of this model: TestModel.objects.create(some_integer=100) TestModel.objects.create(some_integer=50) TestModel.objects.create(some_integer=20) and I'd like to annotate queryset somehow to be able to get following results: for obj in TestModel.objects.annotate(difference=...): print(obj.difference) => 50 # 100 - 50 => 30 # 50 - 30 => None # we don't have anything created after this record Any chance of doing this with Django(2.0) querysets or should I do this 'manually' in Python? -
Grouping ModelForm fields by Model Field Attributes
I've got two models, Question and Project. Project has a ManyToManyField referencing Question: ## models.py class Question(models.Model): category = models.CharField( max_length=4000, blank=True, null=True ) question = models.CharField( max_length=4000, blank=True, null=True ) class Project(models.Model): name = models.CharField( max_length=100, blank=False, ) questions = models.ManyToManyField( Question, blank=True, ) From these I have a CreateView and a custom form assigning the CheckboxSelectMultiple widget to the ManyToManyField. ## views.py class ProjectCreate(LoginRequiredMixin, CreateView): model = Project form_class = ProjectForm ## forms.py class ProjectForm(forms.ModelForm): class Meta: model = Project fields = ['name','questions',] widgets = {'questions':forms.CheckboxSelectMultiple()} So, what I want to do now is group the individual labels and checkboxes for the questions CheckboxSelectMultiple form field according to each question's category. Something along the lines of this (not sure exactly how it would work): {% for field in form.questions %} {% regroup field by <INSERT MYSTERY CODE> as question_list %} {% for category, field in question_list %} {{ category }} {{ field }} {% endfor%} {% endfor %} -
Django - Preferred programming design for forms without violating DRY
I have an app that uses a form that a customer will fill out and submit lets call it formA. Now I also have the same form being used by the staff with some additional fields being rendered. In my forms.py I am forced to include those added fields to the form even though I dont want them to be rendered from the customers point of view or else I cant use the same form for the staff. Currently I have been including logic in the template to ignore these fields that I dont want by using the ifnotequal field.label in a for loop iteration over the fields but I feel as it is becoming too cumbersome and confusing to read. I know another option would have been to create a separate form but then I feel that would violate DRY. Is there a preferred way of reusing forms in this case or a better way of going about this? -
Django : Direct assignment to the forward side of a many-to-many set is prohibited. Use user.set() instead
I am getting this error If I am trying to add current logged in user into another relation as ManyToManyField here is my code TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use user.set() instead. Views.py def add_to_cart(request , item): name = item size = request.POST.get("size") extras = request.POST.get("extras") c = Cart(item=name , size=size , extras=extras , user=request.user) c.save() return render(request , "orders/add_items.html" , {"message":f"{name} added to cart"}) Models.py class Cart(models.Model): item = models.CharField(max_length=64) size = models.DecimalField(max_digits=5,decimal_places=2) extras = models.CharField(max_length=64 , null=True, blank=True) user = models.ManyToManyField(User , related_name="member") def __str__(self): return f"{item} added by {user}" -
Django displaying all users
I want to display all the users, in my template but I cannot. I think there is a mistake in my codes but I cannot find it. Here is codes. views.py class UsersView(TemplateView): template_name = 'users.html' def get_context_data(self, **kwargs): context = super(UsersView, self).get_context_data(**kwargs) context['object_list'] = UserList.objects.all() return context models.py class UserList(AbstractBaseUser): first_name = models.CharField(max_length=200, blank=True, help_text="The first name of the user.") last_name = models.CharField(max_length=200, blank=True, help_text="The last name of the user.") email = models.EmailField( verbose_name='email address', max_length=255, unique=True, help_text="The email and username of the user. Required." ) users.html {% extends 'blog/base.html' %} {% block content %} <h1>Users:</h1> <ul> {% for users in object_list %} <li class="user">{{ user }}</li> {% endfor %} </ul> {% endblock %} And this is the page that I get: Where is my mistake? Thank you. -
How to add multiple parameters in a Django template URL manually
I tried searching for this here, but all the answers that I saw showed only one parameter being passed. Below is my current URL: <a href="{% url 'ad_intel:gallery' %}?usecase={{ dir_data.usecase_base }}/?output={{ dir_data.output_folder }}/?folder_path={{ folder }}/" target="_blank">{{ folder }}</a> I'm trying to pass three variables: usecase, output and folder_path to my view. But, it's identified as only a single parameter. I printed the parameters from my view below: usecase: newspaper/?output=some_output_folder/?folder_path=some_folder/, output: None & folder: None In my view function, I'm using the below code to get the data from the GET request: if request.method=='GET': usecase = request.GET.get('usecase') output = request.GET.get('output') folder = request.GET.get('folder_path') I'm not sure if the syntax I'm using to create the path is correct. My question is if I want to pass more than one parameters to the URL (to be caught by my view) what is the correct syntax for that ? -
Django queryset : Handle table with .distinct() and .count()
I would like to handle some Django tables in order to create a statistic table in my HTML template. For example, I need to get all distinct object from my database table, display the count of each distinct object, ... Example : I have a table named Download which looks like this : #TABLE DOWNLOAD ____________________________________________ | email | PUB (FK_DOCUMENT) | | __________________________________________ | |test12@gmail.com | 1 | | __________________________________________ | |test12@gmail.com | 2 | | __________________________________________ | |test45@gmail.com | 4 | | __________________________________________ | |test22@gmail.com | 3 | | __________________________________________ | |test01@gmail.com | 2 | | __________________________________________ | |test98@gmail.com | 4 | | __________________________________________ | |test74@gmail.com | 4 | | __________________________________________ | This table has a ForeignKey according to table named Document : #TABLE DOCUMENT __________________ | ID | EDQM_ID | | ________________ | |1 | A | | ________________ | |2 | B | | ________________ | |3 | C | | ________________ | |4 | D | | ________________ | I would like to create an HTML table like this : #HTML STATISTICS TABLE ________________________ | PUB_ID | Requests | | _____________________ | |A | 1 | | _____________________ | |B | 2 | | _____________________ β¦ -
UnboundLocalError at /
UnboundLocalError at / local variable 'movie_list' referenced before assignment where am i doing wrong? class ApiListView(TemplateView): def get(self, request): list_view = GetList().get_list_data() movie_list: list_view.json() if request.session.has_key('username'): username = request.session['username'] return render(request, 'loggedin.html', {"username": username,'movie_list':movie_list}) else: return render(request, 'login.html', {'movie_list':movie_list}) -
How to filter a User model with a ForeignKey using custom URL format
Intended URL format: api/v1/users?applicant={applicantId} Current (working) URL format: path('users/applicant/<int:pk>/') How do I check if an applicant has already setup an account on the system as User using the applicantID (which has a foreignkey relationship with the Users table)? models.py: class Applicant(models.Model): APPLICATION_STATUS = ( (1, 'Pending'), (2, 'Accept'), (3, 'Reject'), ) first_name = models.CharField(max_length=200, blank=False, null=False) last_name = models.CharField(max_length=200, blank=False, null=False) email = models.EmailField(max_length=200, blank=False, null=False, unique=True) phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.") phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=True, null=False, unique=True) # validators should be a list linkedin_url = models.URLField(max_length=255, unique=True, blank=True, null=True) #make sure diff users cant use two same profile twitter_url = models.URLField(max_length=255, unique=True) #make sure diff users cant use two same profile articles = ArrayField(models.URLField(), blank=False, null=False, unique=True, size=3) country = models.ForeignKey(Country, on_delete=models.CASCADE, blank=False, related_name="applicant") category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="applicant", blank=False) status = models.CharField(max_length=200, choices=APPLICATION_STATUS, default=1) def __str__(self): return self.first_name class User(AbstractUser): USER_TYPE_CHOICES = ( (1, 'Journalist'), (2, 'Admin'), ) GENDER = ( (1, 'Male'), (2, 'Female') ) first_name = models.CharField(max_length=200, blank=False) last_name = models.CharField(max_length=200, blank=False) # is_active = models.BooleanField(default=True) password = models.CharField(max_length=200) email = models.EmailField(max_length=250, unique=True) phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the β¦ -
how to provide default values in my django app for my users once off?
How to provide a few entries into my django models that shall be same for each of my users. should i use the __init__ or objects.get_or_create function to set up such values...please provide with code samples or links if possible. Thank you -
Django because of CommonMiddleware connection fails?
I ran into a very strange problem. I do unloading from programm. And noticed that sometimes for some reason the file is not uploaded to the Django server. began to dig, put Charly to view the packets being sent. And I saw that Djnago was closing the connection. Package Log Because of this, of course, you can not work with these files. I started digging what could be the problem. Played, and with DATA_UPLOAD_MAX_MEMORY_SIZE, and did len (request.body). Nothing helped, chaotic Django closed the connection. The firewall turned it off, too. I was thrown the idea of disconnecting gradually Middleware. And about the miracle, everything started to go. Package Log In the end, it turned out to be CommonMiddleware. Middleware Next, I tried to find a place that exactly gives an error. I kind of found it. Code If you put a return response before this block, then everything will work fine. Repair code But why exactly because of this place there is a trip I do not understand. Breakpoint did not help, through them passes and closes the connection. Please help, I do not know what to do. Thank you! -
How to use the current user as a method argument in QuerySet property
I have Events A user can subscribe to an Event In views I have a list of Events In the list template I would like have something like event.allready_subscribed available. So that the current user can see that he/she has already subscribed. I can't do this in a model method, because I don't have the request.user available. To do this with .annotate seems a bit overdone. Any other ideas? -
Django-Oscar form fork - Error unknown fields (but fields are in the models)
I am trying to fork django-oscar to change the dashboard form for product attributes, multioption. Need to have a description field for every option. project/oscar_fork/catalogue/models.py: from django.db import models from django.utils.translation import ugettext_lazy as _ from oscar.apps.catalogue.abstract_models import AbstractAttributeOption, AbstractAttributeOptionGroup class AbstractAttributeOption(AbstractAttributeOption): description = models.CharField(_('Description'), max_length=250, blank=True) group = models.ForeignKey( 'catalogue.AttributeOptionGroup', on_delete=models.CASCADE, related_name='optionsblabla', verbose_name=_("Group")) from oscar.apps.catalogue.models import * The models are changed with the extra field "description" in my DB, but still my form field returns cannot find this field. project/oscar_fork/dashboard/catalogue/forms.py: from oscar.apps.dashboard.catalogue import forms as base_forms class AttributeOptionForm(base_forms.AttributeOptionForm): class Meta(base_forms.AttributeOptionForm.Meta): fields = ('option', 'description') If I change the form.py and models.py fields directly in the Oscar app it works. Other forms can be forked that easily as shown above. Tried it with AttributeOptionGroupForm. I think there is a problem with the sequence of import. How can I solve this? Error: django.core.exceptions.FieldError: Unknown field(s) (description) specified for AttributeOption I am using django-oscar v1.6. Django v.2.08. -
Testing function that interacts with Django Rest Framework API using python mock library
I want to unit test a module that interacts with an API built with Django Rest Framework. To do that I'm using python mock library. As I'm not experienced using mocks to test interaction with an API, I'm having problems. The function that sends an object to an API endpoint is this (in short): def send_experiment_to_portal(experiment: Experiment): rest = RestApiClient() if not rest.active: return None # general params params = {"nes_id": str(experiment.id), "title": experiment.title, "description": experiment.description, "data_acquisition_done": str(experiment.data_acquisition_is_concluded), "project_url": experiment.source_code_url, "ethics_committee_url": experiment.ethics_committee_project_url } action_keys = ['experiments', 'create'] portal_experiment = rest.client.action( rest.schema, action_keys, params=params, ) return portal_experiment The RestAPIClient is the class that authenticates to the API: class RestApiClient(object): client = None schema = None active = False def __init__(self): auth = coreapi.auth.BasicAuthentication( username=settings.PORTAL_API['USER'], password=settings.PORTAL_API['PASSWORD'] ) self.client = coreapi.Client(auth=auth) try: url = settings.PORTAL_API['URL'] + \ ( ':' + settings.PORTAL_API['PORT'] if settings.PORTAL_API['PORT'] else '' ) \ + '/api/schema/' self.schema = self.client.get(url) self.active = True except: self.active = False The RestApiClient class is using the coreapi client module to connect to the API. So, at the end, I'm starting from test if the params argument in rest.client.action has the keys/values that are foreseen by the API using python mock library. I'm starting writing β¦ -
Why django-star-ratings not displaying the stars while template loads?
I have added django-star-rating to my app but the template does not display the stars.I have added a rating field in AlbumImage class in models.py, and tried to iterated though a list of images(photos) but the star rating does not appear. I didn't edit the views.py. models.py: class AlbumImage(models.Model): image = ProcessedImageField(upload_to='albums', processors=[ResizeToFit(1280)], format='JPEG', options={'quality': 70}) thumb = ProcessedImageField(upload_to='albums', processors=[ResizeToFit(300)], format='JPEG', options={'quality': 80}) album = models.ForeignKey('album', on_delete=models.PROTECT) alt = models.CharField(max_length=255, default=uuid.uuid4) created = models.DateTimeField(auto_now_add=True) width = models.IntegerField(default=0) height = models.IntegerField(default=0) slug = models.SlugField(max_length=70, default=uuid.uuid4, editable=False) ratings = GenericRelation(Rating, related_query_name='object_list') def __unicode__(self): return self.ratings views.py: class AlbumDetail(DetailView): model = Album def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(AlbumDetail, self).get_context_data(**kwargs) # Add in a QuerySet of all the images context['images'] = AlbumImage.objects.filter(album=self.object.id) return context template: {% load ratings %} {% for item in object_list %} {{item.image}} and {{item.ratings}} {% endfor %} -
Django many-to-many stores reverse relation as separate entry
I have a model called Cell that has a many-to-many relation with itself, called combined_from. class Cell(models.Model): combined_from = models.ManyToManyField('self', related_name='combined_into', blank=True) ... What I'm trying to model is that a cell can be combined from multiple other cells, and that cells know they're combined into another cell (this is an end of life situation for these cells). So, when I combine 3 cells (let's call them c1, c2, c3), I create a new one (called c4) and I do c4.combined_from.add(c1, c2, c3). So far so good, but the problem I have is that when I look at c1.combined_from.all(), I can see c4 appearing there! I verified this behaviour with a minimal example in my terminal >>> from cells.models.models import * >>> from MCH4.utils.debug.queryrecorder import record_queries >>> c1 = Cell.objects.first() >>> c2 = Cell.objects.last() >>> c1.combined_from.clear() >>> c2.combined_from.clear() >>> c1.combined_from.count() 0 >>> c2.combined_from.count() 0 >>> c1.combined_from.add(c2) >>> c1.combined_from.count() 1 >>> c2.combined_from.count() 1 Any idea of how thus might happen or even how I can avoid this from happening again. I'm using django 1.11 if this is relevant information. -
How to retain data in production db when making migrations in Django
I am currently developing a Django site, where I want the development database and the production database separate. I want to be able to have a production database which can be migrated without losing the data stored in it. When I create new models in models.py, I need to makemigration and migrate on my development database. I have tried makemigration on development server, pushing the migration-file to server, and making the migrate on the server. This overwrites my data in the production database. What is the best workflow to update the production database with the new migrations - without loosing the data in the production database? -
Query which spans and follows back several tables
My question is about creating a query which filter objects that are related through several intermediate tables. My relational database looks like this: Any number of products can be uploaded by one user (one to many relationship). However, users also can rank products. A ranking can be completed by several users and a user can have several rankings (Many to many relationship). The same applies between Product and Ranking. I use explicit intermediate tables (Rank and Belong) which defines the M2M relationships by the through parameter, because they have additional information which describes the relationship. The models code is something like this (I omitted irrelevant fields for simplicity): class Product(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) belong= models.ManyToManyField(Ranking, through="Belong") #... #The M2M table which relates Product and Ranking class Belong(models.Model): ranking = models.ForeignKey(Ranking, on_delete=models.CASCADE) product = models.ForeignKey(Product, on_delete=models.CASCADE) created = models.DateTimeField(auto_now_add=True) #... class Meta: unique_together = (("ranking", "product")) class Ranking(models.Model): user= models.ManyToManyField(settings.AUTH_USER_MODEL, through="Rank") created = models.DateTimeField(auto_now_add=True) #... #The M2M table which relates User and Ranking class Rank(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) ranking = models.ForeignKey(Ranking, on_delete=models.CASCADE) created = models.DateTimeField(auto_now_add=True) #... class Meta: unique_together = (("user", "ranking")) #The AUTH_USER_MODEL, which is not included here My question is: How can I create a query which β¦ -
Django memoize per request
I have a django rest application in which there is a function which calculates something using some external data (from another service) I want to avoid loading if not required. This data changes regularly, but is the same for one request. Therefore, I want to cache the result of this function (since it is called multiple times during one request) for the duration of exactly one request. I've come across the https://github.com/tvavrys/django-memoize/ library, which caches function results, but I can only specify a time and not a context after which the cache should be invalidated. One possibility I found reasonable is to somehow register a hook which clears the cache after every request (using delete_memoized), but I have not found a method to register such a hook. Therefore, my question is: Is it possible to either execute some code after a response has been rendered (→ clearing the cache), or tell django to cache a function result for exactly one request (using some other library?) -
Django template language - accesing value from dictionary based on key
i have a question: Let's say, that i have a value stored in variable in Django template language like so: {{ user.username }}, let's say, that this variable has value "bob123". Then i have a dictionary called {{ people }} and in that dictionary are keys, and values like this: {'bob123': 'Employee', 'johny234': 'Employer'}. I want to get the value from this dictionary based on user.username. I know that i can do this and get the value {{ people.bob123 }}(it worked), but if i do this {{ people.user.username }} it doesn't work. Is there an option to do it somehow, or do i have to make some custom template tag for it? If it isn't necessary i don't want to make custom template tag. -
Django 2.1 TypeError: __init__() got an unexpected keyword argument 'on_delete'
class UserProfile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='profile') following = models.ManyToManyField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='followed_by') def __str__(self): return str(self.following.all().count())