Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:45:20+00:00 2026-06-14T00:45:20+00:00

I need to let a user create an Event which has a Category .When

  • 0

I need to let a user create an Event which has a Category.When a user goes to the create_event page he is shown a dropdown list of Category instances.I need to make sure that only those Category s created by the user should be shown in the dropdown list

I tried to subclass Form for this sothat I can use it as below in view and template.

template for create_event:

<h3>select from existing categories</h3>
{{category_choices_form.as_p}}

view for create_event:

def create_event(request,..):
    user_categories = Category.objects.filter(creator=request.user)
    form_data = get_form_data(request)
    category_choices_form = CategoryChoicesForm(request.user,form_data)# is this correct?
    ...

def get_form_data(request):
    return request.POST if request.method == 'POST' else None

Then I created the Form subclass

class CategoryChoicesForm(forms.Form):
    def __init__(self, categorycreator,*args, **kwargs):
        super(CategoryChoicesForm, self).__init__(*args, **kwargs)
        self.creator=categorycreator
    categoryoption = forms.ModelChoiceField(queryset=Category.objects.filter(creator=self.creator),required=False,label='Category')

However, the line starting categoryoption = causes error saying name 'self' is not defined

Can somebody help me with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:45:21+00:00Added an answer on June 14, 2026 at 12:45 am

    When you have a model that you want to expose as an html form, always prefer
    to use a ModelForm instead of a simple form. Unless of course you’re doing
    a lot of weird or complicated stuff and building a simple form is more straightforward.

    So in your situtation

    from django.forms import ModelForm
    
    class CategoryForm(ModelForm):
        class Meta:
            model = Category
            # exclude = ('blah', 'foo') # You can exclude fields from your model, if you dont want all of them appearing on your form.
    

    Your get_form_data function is unnecessary. This is how you use your form in a view

    from django.template import RequestContext
    from django.shortcuts import render_to_response
    
    def create_event(request,..):
        if request.method == 'POST':
            form = CategoryForm(request.POST)
            if form.is_valid():
                # do stuff and redirect the user somewhere
    
        else:
            # We're in a GET request, we just present the form to the user
            form = CategoryForm()
        return render_to_response('some_template.html', {'form':form}, context_instance=RequestContext(request))
    

    About the self is not defined error:

    in your CategoryChoicesForm you have this line

        categoryoption = forms.ModelChoiceField(queryset=Category.objects.filter(creator=self.creator),required=False,label='Category')
    

    This is at the class level, when self can be used only on an instance level. self is not “visible” there. It would be visible inside a method of CategoryChoicesForm though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple application in which I need to let the user select
I have a C# project using windows forms. I need to let the user
Let me explain: I need to sort a list in ascending order, while results
Some background The system in question has some ten thousand records which the user
Hopefully this will make sense. I have a page where an user can create
I create a user control group sub-class. There are two radiobuttons. I need to
Basically in order to have a profile Block u need - let's say profile
I need to let users of an application open a folder by clicking a
I have a cart on my website and I need to let users easily
In my web application, I need to let users (mostly IE browsers) to click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.