How do I initialize a ForeignKey ModelChoiceField in Django?
Tag : django , By : DarrenBeck
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Take a look at Overriding the default field types or widgets. Based on this I would try the following. from django.forms import ModelForm, Select
class MyOtherModelForm(ModelForm):
my_model=ModelChoiceField(queryset=MyModel.objects.all(), widget=Select(attrs={'style':'background_color:#F5F8EC'}))
class Meta:
model = MyOtherModel
|
Grouping django objects using QuerySet API by shared text tags using two models with ForeignKey relationship
Tag : python , By : littlefuzz
Date : March 29 2020, 07:55 AM
Hope this helps You'd better create two additional models: one for Tag and one for link between Tag and Document. If it's somewhy unacceptable, you can use something like: Document.objects.filter(tagdb__tag_text__in=doc.tags_as_csv.split(' , ')).distinct()
|
Django Forms and ModelChoiceField: Add HTML optgroup element to specific objects in QuerySet?
Date : March 29 2020, 07:55 AM
I wish this helpful for you Here is my current solution. It might be a dirty fix, but it works fine :-) class CustomCourseWidget(forms.Select):
#http://djangosnippets.org/snippets/200/
def render(self, name, value, attrs=None, choices=()):
from django.utils.html import escape
from django.utils.encoding import smart_unicode
from django.forms.util import flatatt
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, name=name)
output = [u'<select%s>' % flatatt(final_attrs)]
output.append(u'<option value=""></option>') # Empty line for default text
str_value = smart_unicode(value)
optgroup_open = False
for group in self.choices:
option_value = smart_unicode(group[0])
option_label = smart_unicode(group[1])
if not ">" in option_label and optgroup_open == True:
output.append(u'</optgroup>')
optgroup_open = False
if not ">" in option_label and optgroup_open == False:
output.append(u'<optgroup label="%s">' % escape(option_label))
optgroup_open = True
if " > " in option_label:
#optgroup_open = True
selected_html = (option_value == str_value) and u' selected="selected"' or ''
output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(option_label.split(" > ")[1])))
output.append(u'</select>')
return mark_safe(u'\n'.join(output))
|
Django - ModelChoiceField queryset based on a parent ModelChoiceField
Tag : django , By : Ivan Belov
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further From here, I ended up using self._raw_value('parent') which solved my problem. Update: Django 1.9 removed _raw_value(), here is the alternative: self.fields['parent'].widget.value_from_datadict(
self.data, self.files, self.add_prefix('parent')
)
|
Django - Filter queryset with child objects (ForeignKey)
Date : March 29 2020, 07:55 AM
seems to work fine I have 3 Models, and 2 of them correspond to the first one. , Yes: from django.db.models import Q
children1 = Child1.objects.filter(blah=blah)
children2 = Child2.objects.filter(blah=blah)
parents = Parent.objects.filter(Q(child1__in=children1) | Q(child2__in=children2))
|