django-forms

Django CheckboxSelectMultiple override 'choices' from ModelForm

怎甘沉沦 提交于 2020-01-04 15:15:31
问题 I would like to be able to extract different information in my django form: That's my form: <form action="" method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" value="Submit" /> </form> class InstanceForm(ModelForm): class Meta: model = models.BaseAsset widgets = { 'labels': LabelIconCheckboxSelectMultiple() } The model: class AssetClass(models.Model): default_labels = models.ManyToManyField(Label, null=True, blank=True) pass the M2M reference field class Label(models.Model):

Display forms choice in template-Django

一世执手 提交于 2020-01-04 14:08:30
问题 On the template, when I call person.health_issue, I am getting '1','2' instead of 'Abdominal pain','Anaphylaxis'. How to display the value ('Abdominal pain','Anaphylaxis') instead of the code(1 or2 etc). I tried with this also {{ person.get_health_issue_display }} in template,it is not displayed anything. forms.py HEALTH_USSUES = ( ('1', 'Abdominal pain'), ('2', 'Anaphylaxis'), ('3', 'Asthma'), ('4', 'Bruising'), ('5', 'Chest pains'), ('6', 'Coughs or Colds') ) class PersonActionsForm(forms

ForeignKey Field in django admin

家住魔仙堡 提交于 2020-01-04 11:01:53
问题 There are around 1.5 lakhs entry in User model. So when i am using it in django-admin without the raw_id_fields it is causing problem while loading all the entry as a select menu of foreign key. is there alternate way so that it could be loaded easily or could become searchable. Basically i have these models as of defined above and there is a User model which is used as ForeignKey in ProfileRecommendation models. so the database entry for user model consist of around 1,50,000 entries. I don't

How to assign initial value to a custom fields in ModelAdmin?

放肆的年华 提交于 2020-01-04 05:47:21
问题 I am trying to extends django user auth through onetonefield in model. Here is the model: from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) nim = models.CharField(max_length=10, blank=True) def __str__(self): return self.user.username With basic admin panel i get to create user instance first and

How to populate html table with info from list in django

跟風遠走 提交于 2020-01-04 05:12:34
问题 I want to populate, in my django application, my table from base.html with the results from urlparse.py (this returns a list of 20 URLs from a site). models.py from django.db import models from django.utils.encoding import smart_unicode # Create your models here. urlparse.py import HTMLParser, urllib2 class MyHTMLParser(HTMLParser.HTMLParser): site_list = [] def reset(self): HTMLParser.HTMLParser.reset(self) self.in_a = False self.next_link_text_pair = None def handle_starttag(self, tag,

How to populate html table with info from list in django

南楼画角 提交于 2020-01-04 05:12:10
问题 I want to populate, in my django application, my table from base.html with the results from urlparse.py (this returns a list of 20 URLs from a site). models.py from django.db import models from django.utils.encoding import smart_unicode # Create your models here. urlparse.py import HTMLParser, urllib2 class MyHTMLParser(HTMLParser.HTMLParser): site_list = [] def reset(self): HTMLParser.HTMLParser.reset(self) self.in_a = False self.next_link_text_pair = None def handle_starttag(self, tag,

Display a red asterisk (*) in Django forms

本秂侑毒 提交于 2020-01-04 03:49:07
问题 I want to change in my form the color of the label_suffix. I just want to set the '*' in red and leave the rest black. Is this possible or do i have to change something in my HTML? username = forms.CharField(label="Username",label_suffix='*') 回答1: Get rid of this - label_suffix='*' . We'll write some CSS to display a * after the required fields. First, in your form set an attribute called required_css_class: class MyForm(...): required_css_class = 'required' Django will set a class called

When to use django forms vs manual forms?

落花浮王杯 提交于 2020-01-04 02:28:06
问题 I am beginning learning django and have just come across django forms (I've always used manual forms in the past in django templates). Should I always use django forms no matter what or are there scenarios where I should write the forms in the templates manually? 回答1: You can use your own forms, but it is better to use django forms because it provides more customization and flexibility. It is easy to change the type of "widget" rendered with very little code change when you use django forms.

Django Multilingual Text Field using JSON

限于喜欢 提交于 2020-01-03 17:18:15
问题 I recently ask this question Custom Django MultilingualTextField model field but I found no good reason why I should not do this, so I create a model Field that support multilingual text, auto return text in current language. This basically is the field that store custom Language object to database in json format. Here is the code: Github: https://github.com/james4388/django-multilingualfield Ussage: from django.db import models from multilingualfield import MLTextField, MLHTMLField class

Unable to save with save_model using database router

送分小仙女□ 提交于 2020-01-03 13:32:31
问题 I am using a database router hence i have two databases for my application. One database for default django data and the other one. In my admin i have override the save_model function in order to save the created_by variable, but i am unable to do this. Cannot assign "<User: testuser>": the current database router prevents this relation. database router: from django.conf import settings class DatabaseAppsRouter(object): def db_for_read(self, model, **hints): """Point all read operations to