django-widget

Customize ClearableFileInput in django template

≯℡__Kan透↙ 提交于 2021-02-19 05:10:27
问题 I've got a form with profile_picture=ImageField field set to the initial value. It's using ClearableFileInput widget. I need to customize the form in the template, so I can't simply use {{ form.profile_picture}} . How can I split field elements and obtain something which looks like this: {{ with picture=form.profile_picture }} {{ picture.label_tag }} <a href="{{ picture.url }}"> <img src="{{ picture.url }}"> </a> {{ picture.clear-picture }} where {{ picture.clear-picture }} should generate

Customize ClearableFileInput in django template

天涯浪子 提交于 2021-02-19 05:10:03
问题 I've got a form with profile_picture=ImageField field set to the initial value. It's using ClearableFileInput widget. I need to customize the form in the template, so I can't simply use {{ form.profile_picture}} . How can I split field elements and obtain something which looks like this: {{ with picture=form.profile_picture }} {{ picture.label_tag }} <a href="{{ picture.url }}"> <img src="{{ picture.url }}"> </a> {{ picture.clear-picture }} where {{ picture.clear-picture }} should generate

Django admin custom ArrayField widget

末鹿安然 提交于 2020-02-20 07:32:09
问题 The current admin widget for ArrayField is one field, with comma as delimiter, like this (text list): This isn't ideal because I would have longer texts (even 20 words) and contain commas. I could change the delimiter to be something else but that still doesn't help with unreadable content in admin. What I would like is having a list of fields, that I can alter in admin. Something similar to the following image I could use another table to solve this, but I wonder if it's possible to solve it

django: SplitDateTimeWidget ignores date_format

风格不统一 提交于 2020-01-06 19:41:35
问题 I am trying to use the SplitDateTimeWidget but want it to accept date in day - month - year format. from django.forms.widgets import SplitDateTimeWidget class EventForm(forms.ModelForm): class Meta: model = Event widgets = {'start': SplitDateTimeWidget(date_format='%d/%m/%Y')} The SplitDateTimeWidget accepts a date_format argument, which I expect to be used to validate the date input but it isn't. The default widget is correctly replaced but it ignores the date_format and insists on

How to get a single widget to set 2 fields in Django?

穿精又带淫゛_ 提交于 2020-01-01 06:46:46
问题 I got a model with 2 fields: latitude and longitude. Right now they're 2 CharFields, but I want to make a custom widget to set it in admin - was thinking about displaying Google Maps, then getting the coordinates of the marker. But can I have 1 widget (a single map) to set 2 different fields? 回答1: Define lat and long fields on your form; set them to use the HiddenInputWidget. After the init of your form, add another field which contains your custom widget that takes two values. In the clean

Django - Show BooleanField in a formset as one group of radio buttons

為{幸葍}努か 提交于 2019-12-30 06:37:43
问题 I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) def primary_phone(self): return self.phone_set.get(primary=True) class Phone(models.Model): profile = models.ForeignKey(Profile) type = models.CharField(choices=PHONE_TYPES, max_length=16) number = models.CharField(max_length=32) primary = models.BooleanField(default=False) def save(self, force_insert=False, force_update=False, using=None): if self.primary: # clear the primary attribute of

Django: Check multiple choices with not fixed choices

笑着哭i 提交于 2019-12-25 07:35:39
问题 I am new to Django, and I am trying to create a multiple selection with checkboxes. The problem is that all of the examples that I found have fixed choices that are specified in the form, and I don't need that. More concretely, let this be a model for a simple car dealership app: class CarBrand(models.Model): name = model.CharField() class CarModel(models.Model): name = model.CharField() brand = model.ForeignKey(CarBrand) My goal is when I enter the page for Audi, I get options A3, A4, A5,

How to create a custom date time widget for the django admin?

非 Y 不嫁゛ 提交于 2019-12-24 01:58:10
问题 My Problem: I have a model that accepts DateTimeField . The user enters this from the django-admin . But, I cant get a way to get the user's local timezone. So my best shot is forcing the user to enter the date-time in UTC . But for the users convenience, I do not want him/her to calculate each time the offset and then the time in UTC . Django doesn't seem to provide a way of doing this. What I intend to do: I want to create a custom widget that will also let the user choose the timezone

Django ModelForm with Select Widget - Use object.uid as default option value instead of object.id

故事扮演 提交于 2019-12-22 08:25:18
问题 I have a form inheriting from ModelForm as such: class ChildModel(ModelForm): class Meta: model = Documents fields = ('secretdocs') widgets = { 'secretdocs': Select(attrs={'class': 'select'}), } The model "secretdocs" has a uid. But when it prints out the select and option, the option values appear as such: <select class="select" id="id_secretdocs" name="secretdocs"> <option value="1">My Secret Doc</option> </select> But I want it to instead have the uid of the option: <select class="select"

Django admin GenericForeignKey widget

非 Y 不嫁゛ 提交于 2019-12-22 07:55:14
问题 I'm creating a Django app where all the models can be related to each other in an order set by the user. I'm setting all this up using GenericForeignKeys. The kicker is that I need to be able to support multiple collections of these types of relationship/admin. So one object can have a more than one collection of related objects. Does anyone know of a good GenericForeignKey widget for this situation? Preferably, it would be an autocomplete search that populates the admin form since I can end