Django: Access primary key in models.filefield(upload_to) location

I'd like to save my files using the primary key of the entry. Here is my code: def get_nzb_filename(instance, filename): if not instance.pk: instance.save() # Does not work. ...

Django admin: exclude field on change form only

If there a way to detect if information in a model is being added or changed. If there is can this information be used to exclude fields. Some pseudocode to illustrate what I'm talking...

Unique BooleanField value in Django?

Suppose my models.py is like so: class Character(models.Model): name = models.CharField(max_length=255) is_the_chosen_one = models.BooleanField() I want only one of my Character...

Showing custom model validation exceptions in the Django admin site

I have a booking model that needs to check if the item being booked out is available. I would like to have the logic behind figuring out if the item is available centralised so that no matter...

Adding extra constraints into fields in Django

While subclassing db.models.Model, sometimes it's essential to add extra checks/constraints. E.g. I have an Event model with start_date and end_date. I want to add validation into the fields or...

How to make a Django form retain a file after failing validation

form = AddItemForm(request.POST, request.FILES) if form.is_valid() do_stuff return render_to_response(blah.html, {'form':form}) Now form will have the error information along with the...

How to get form fields' id in Django

Is there any way to get the id of a field in a template? In the HTML I get: <input name="field_name" id="id_field_name"... I know I can get the name with {{ field.html_name }}, but is there...

Align radio buttons horizontally in django forms

HI I want to align the radio buttons horizontally. By default django forms displays in vertical format. feature_type = forms.TypedChoiceField(choices = formfields.FeatureType, widget =...

Difference between cleaned_data and cleaned_data.get in Django

I've seen some samples codes like: def clean_message(self): message = self.cleaned_data['message'] num_words = len(message.split()) if num_words < 4: raise...

Access parent object fields of save_formset and save_model

I have a ActionAdmin model, which is used as ActionAdminInline in the InvoiceAdmin model. In InvoiceModel i override save_formset, to auto set some values. Setting the values for each Action...

Pass additional parameters to post_save signal

I have a user registration form in my Django application which collects additional data while a user is trying to register such as address, city, country, phone number etc. This data is saved in...

Create empty queryset by default in django form fields

I have this fields in form: city = forms.ModelChoiceField(label="city", queryset=MyCity.objects.all()) district = forms.ModelChoiceField(label="district", queryset=MyDistrict.objects.all()) area =...

Django admin does not login properly with custom User model

I upgraded an app I had on Django 1.4.5 to Django 1.5 and just finished migrating over to a custom User model. When I login to my app, using my own authentication form, with my superuser...

uploading files with urllib to a FileField model in Django

I have a django file model that has a models.FileField field and a form that is used to upload files to the server: class UploadFile(model.Model): filename =...

Checking if username exists in Django

I am working on a Django project where users will be able to change their usernames along with their first and last name in one form. In forms.py, I am trying to find out if the user exists. If...

Why is it common to put CSRF prevention tokens in cookies?

I'm trying to understand the whole issue with CSRF and appropriate ways to prevent it. (Resources I've read, understand, and agree with: OWASP CSRF Prevention CHeat Sheet,...

Django: storing user preferences

I am still very new to Django and trying to figure out what is the best approach for the following problem. I want to store user personal preferences (things user likes or dislikes) as simple...

Add class to form field Django ModelForm

I am trying to write a Bootstrap Form with Django ModelForm. I have read the Django Documentation Django Documentation about Forms, so I have this code: <div class="form-group"> {{...

Render title/legends as html with django crispy forms

I got a form with tabs, and an accordion under a tab, like this: self.helper.layout = Layout( TabHolder( Tab( 'Title', HTML('{%...

Django: Order_by multiple fields

I am getting order_by fields in the form of a list. I want to order_by by multiple fields with django orm. List is like below: orderbyList = ['check-in','check-out','location'] I am writing a...

Django-Filter Form Displaying All Filters?

I've just started working with Django Filter. When I test, the filter.form shows filters for all fields and I can't get it to show only the desired filters. Here's the filter: class...

Django: Access Model fields in template when using form.ModelMultipleChoiceField

It is a follow up to this question and answer. My question now is, when I have a ModelMultipleChoiceField in my form how do I access all of the model fields of a passed queryset in...

Creating django form with null and blank field

I'm trying to create a form where both fields is optional however, i keep getting an error when setting null and blank. what am i doing wrong? Error super(CharField, self).__init__(*args,...

tracking opened email via django

Hi all I tried to track the opened mails with PHP but google's new image proxy thing now not allowing to execute php scripts via mail(May be my imagination if someone had a working script pls...

Django forms: how to show only objects associated with user in dropdown

I am a vet hospital, with class Pet and class Records. Each pet can have many records, i.e. everytime it visits the hospital it gets a new record. At the moment, my form shows all the pets ever...

Django ckeditor - set width to 100%

I've got a simple django-ckeditor implementation with default settings set as CKEDITOR_CONFIGS = { 'default': { 'toolbar': [ ['Undo', 'Redo', '-', 'Bold',...

django model select_related or prefetch_related child model

Newbie here, I have two models which are below class ReceipeMaster(models.Model): receipe_type = models.CharField(max_length=50, choices=TYPE_OPTIONS, default=TYPE_OPTIONS[0]) item =...

how can i get the "post id"(foreign key field connected with comment model) to the django comments form without post_detail page in django

I am creating a single page blog site how can i get the post id to the comments form , I created django forms with necessary field but the problem is ,I have to select the post id from a drop down...

Forbidden (403) CSRF verification failed. Request aborted. Reason given for failure: Origin checking failed does not match any trusted origins

Help Reason given for failure: Origin checking failed - https://praktikum6.jhoncena.repl.co does not match any trusted origins. In general, this can occur when there is a genuine Cross Site...

Access cleaned data from django admin for pdf processing

Hi there I'm fairly new to django so please be kind if anything sounds a bit newbie to you. I am currently trying to add a print button to every existing django admin view of a project so it will...