logo
down
shadow

django queryset to output foreignkey objects to ModelChoiceField


django queryset to output foreignkey objects to ModelChoiceField

Content Index :

django queryset to output foreignkey objects to ModelChoiceField
Tag : django , By : Jonathan Bernard
Date : January 12 2021, 09:11 PM

should help you out , You can query with:
Location.objects.filter(point__isnull=False).distinct()

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

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?


Tag : python , By : amy
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)


Tag : django , By : Bruce
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))
Related Posts Related QUESTIONS :
  • Data not showing on template
  • Django 2.2 set ModelChoiceField initial value without id/pk
  • Django REST Framework Deep Dive - Where is it determined that an enpoint needs an auth token
  • Keeping items per page static while paginating
  • Need help on nested Django Rest Framework
  • Wagtail RichTextField not showing in Django ModelForm
  • Date Validation --> end date must be greater than start date
  • Nginx, SPA & Sphinx Doco Authentication
  • Django reverse 'str' object has no attribute 'get'
  • Unknown interpreted text role "setting" with Django docstring and Sphinx
  • Django CONN_MAX_AGE is set to 0 but connection remains open after executing query
  • Error message 'mkvirtualenv is not recognized as an internal or external command'
  • How to use PythonAnywhere SFTP to store uploaded files to get past file size limitations in Django Filer?
  • Django REST Framework: Why Adding IsAuthenticated Permissions Only Threw 500 Internal Error Instead of 401 / 403 Errors?
  • Django Admin: Restrict staff to update
  • Django 2.2 Time and Date Localization Specifics
  • FOREIGN KEY constraint failed because of models.DO_NOTHING
  • How To Remove Row in django-extra-views?
  • django select_related('created_by').... what is the context variables?
  • Django Admin - Upload multiple images to multiple model objects in a single upload
  • Wagtail PASSWORD_REQUIRED_TEMPLATE is not overriding the default login
  • get checkbox items list from table in django without using django-forms
  • Django Running Particular Test
  • How do I display output of a python within Html in django?
  • How can I capture errors and exceptions data from frontend response to django to add more context
  • Django generate group by different than id
  • populate table with initial data
  • Django (admin.e104) must inherit from 'InlineModelAdmin'
  • how to display a html file stored in s3 in aws in my django template?
  • Django: AttributeError: type object 'GroupModel' has no attribute '_meta'
  • SQLite objects created in a thread can only be used in that same thread with Django 2.2.2 and ipdb
  • Django - query filter on manytomany is exists
  • Django REST Framework: URLs without PK
  • How to access a Postgres database from a python program without django or heroku
  • When trying set corsheaders in settings.py file
  • 400(Bad request) status code when making request with axios in reactjs
  • ERR_CONNECTION_REFUSED with Django and Vue.js bundle files
  • LDAP Authentication not working in Graphite
  • send value from anchor tag to views
  • The views didn't return an HttpResponse object. It returned None instead
  • Sum of related objects across 2 FK to the same table, with conditions
  • Queryset Django - according to parameters date in weight compartment
  • how to convert Django 2.0 url() to path()
  • Signal/Method for every executed sql statement
  • How to sum django model fields
  • django template styntex {% if p.category == "help" %} {% endif %} is not work
  • NGINX 502 bad gateway gunicorn timeout
  • How to pass a variable from a template to a view in Django
  • How to force redirect_uri to use HTTPS with python-social-app?
  • How to restrict Django models from using certain foreign key values?
  • Product items is not iterable from models
  • Removing items from celery_beat doesn't remove them from database schedule
  • Column field is not recognized/not defined
  • AttributeError at has no attribute 'objects'
  • how to Validate Expire date is bigger than start date in Django Model Form
  • Django Application
  • How to get my Django app in a real server?
  • How do I run a unit test against the production database?
  • How to browse (no text editor) .rst files?
  • Filter Queryset in Django inlineformset_factory
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com