logo
down
shadow

How to display the data contained in Django models in a Django Dropdown menu form?


How to display the data contained in Django models in a Django Dropdown menu form?

By : Tony Knife
Date : November 20 2020, 03:01 PM
it fixes the issue You'll have to write it differently. To get all the customer names you'll have to define it in its own function. That way you can generate the choices that way.
code :
def customer_names():
    list_of_customers = []
    querySet = Customer.objects.all()
    for customer in querySet:
        list_of_customers.append(customer.first_name + " " + customer.last_name)
    return list_of_customers


class DropDownMenuCustomer(forms.Form):

    customer= forms.ChoiceField(choices=[(x) for x in customer_names())])


Share : facebook icon twitter icon
Display form in Django not based on Models/Form

Display form in Django not based on Models/Form


By : Gin
Date : March 29 2020, 07:55 AM
it helps some times You could use forms that aren't associated with models, like this:
code :
class QuestionForm(forms.Form):
    answer = forms.CharField()

def questions(request):
    if request.method == 'POST':
        form = QuestionForm(request.POST) 
        if form.is_valid():
            # Process the data in form.cleaned_data
            return HttpResponseRedirect('/done/')
    else:
        form = QuestionForm() # An unbound form

    return render_to_response('questions.html', {'form': form,})
How to display modelforms from dropdown menu django

How to display modelforms from dropdown menu django


By : Steve Vwj
Date : March 29 2020, 07:55 AM
will help you Take a look on smart_selects, it allows populating one select with constraint on another's value.
django form dropdown list of stored models

django form dropdown list of stored models


By : ludjac
Date : March 29 2020, 07:55 AM
it should still fix some issue You should use ModelChoiceField.
code :
class CronForm(forms.Form):
    days = forms.ModelChoiceField(queryset=Books.objects.all().order_by('name'))
def show_book(request):
   form = CronForm()
   if request.method == "POST":
      form = CronForm(request.POST)
      if form.is_valid:
         #redirect to the url where you'll process the input
         return HttpResponseRedirect(...) # insert reverse or url
   errors = form.errors or None # form not submitted or it has errors
   return render(request, 'path/to/template.html',{
          'form': form,
          'errors': errors,
   })
book_form = BookForm() # This will create a new book
book = get_object_or_404(Book, pk=1)
book_form = BookForm(instance=book) # this will create a form with the data filled of book with id 1
Populate django form using dropdown menu for two fields

Populate django form using dropdown menu for two fields


By : user2047507
Date : March 29 2020, 07:55 AM
it fixes the issue forms.py , In Your Form use a form.ChoiceField instead of a CharField().
code :
from django import forms

INS_CHOICES = (
    ('NIT', 'Netaji Subhas Institute of Technology, New Delhi'),
    ('NSIT', 'Netaji Subhas Institute of Technology, New Delhi'),
    ...
)

class DataForm(forms.Form):
    ins = forms.ChoiceField(choices=INS_CHOICES)
Return data contained in two different models in django rest framework

Return data contained in two different models in django rest framework


By : anniekim
Date : March 29 2020, 07:55 AM
will help you First of all in Django rest framework you must work with related name in models. Rewrite models as:
models.py
code :
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE,primary_key=True, related_name = 'user_profile')
class ProfileUserCombinedSerializer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(), required=False)
    class Meta:
        model = Profile
        fields = ("user","bio",)

class UserSeralizer(serializers.ModelSerializer):
    user_profile = ProfileSerializer()
    class Meta:
        model = User
        fields = ('username', 'password',"email","first_name","last_name","user_profile",)
Related Posts Related Posts :
  • mock twilio rest client for testing
  • Django Class Based View with get_queryset()
  • Django Rest Framework error: {"user":["This field is required."]
  • join two queries together as one - django
  • Django query ForeignKey Count() zero
  • Django: how to get the request object in urls.py
  • db_index in django as raw sql query
  • Removing unnecessary filters from URL django-filter
  • Is there a standard way to mock Django models?
  • My form fields are no longer coming though on the update page?
  • Filtering select field options in django-admin
  • Trying to use Docker with Django but server not starting
  • Does a foreign key object have a unique id value?
  • How to render/retrieve "field[]" in django forms?
  • TzInfo error when unsing Func() on a DatetimeField
  • DJango authentication User search not working properly
  • How to rename Table name in Django?
  • send just a specific filed on ModelSerializer to client - django rest framework
  • Deploying Django app on Ubuntu 16.04 apache2 with mod_wsgi
  • Django Rest Framework - Override serializer create method for model with self-referential M2M field
  • Running cookiecutter django on an intranet
  • Django wagtail getting page children with levels
  • How to delete an object after 24hrs since creation of the same
  • Django Using Slug Field for Detail URL
  • Vue and Django Development Environment
  • DRF Serializer handling generated field
  • How to use django-phonenumber_field in forms.py?
  • Django default at database
  • request.FILES empty, though file is present in request
  • Django: Not fetching the same object
  • Add Billing Address Validation in Stripe.js
  • get any instance (if exist) or create new for any child of abstract model
  • How to configure Django with Webpack/React using create-react-app on Ubuntu server
  • post_save doesn't list tags
  • Doubts regarding Django views
  • Django many-to-many add having inconsistent effect in functions, works on console
  • django restful api - how to serialize foreign keys
  • Target WSGI script cannot be loaded as a python module and ImportError: No module named 'django'
  • How to put session data in SetUp() - Django testing
  • How to redirect from html page to view and post data in Django
  • Access pk from related model using prefetch and to_attr
  • Image does not upload using createview from template django
  • How to exclude values in django aggregation?
  • UpdateView problem : cleaned data passed in url not in instance
  • Using template tag in ModelAdmin.readonly_fields method
  • Fix AttributeError: 'str' object has no attribute 'HTTP_201_CREATED'
  • modelformset_factory doesnt save instances to database
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org