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

  • Home
  • SEARCH
  • 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 6164713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:02:23+00:00 2026-05-23T22:02:23+00:00

I have a model named Domain which looks like this: class Domain(models.Model): Model for

  • 0

I have a model named Domain which looks like this:

class Domain(models.Model):
    """
    Model for storing the company domains
    """
    user = models.ForeignKey(
        User
    )
    host = models.CharField(
        null=False, verbose_name="Host", max_length=128, unique=True
    )

I’d like to use Django’s generic views for doing CRUD operations on this. There is one field in this model that needs user input but the foreign key field doesn’t need any user input. How can I exclude that field from the form that my generic view generates but assign it the value of the current authenticated user.

Thanks.

  • 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-05-23T22:02:24+00:00Added an answer on May 23, 2026 at 10:02 pm

    Have a look at Russel’s answer to a similar question on the django-users group earlier this week.

    Quoting the answer*:

    Forms and Views solve different problems.

    The View is solving the problem of “how do I handle this request and
    convert it into a response?”. The Form is solving the problem of “How
    do I convert the POST data in this request into a model object (or a
    change to a model object)?”.

    Very roughly, a view is doing the following:

    1. View gets a request
    2. View works out whether this is a GET or a POST
    3. If its a POST, View asks the Form to turn the Post into a model change
    4. Form returns success or failure
    5. View responds to the success or failure of the Form.
    6. View returns a response.

    The functionality of the Form is a complete subset of the
    functionality of the View — and for this reason, it’s a completely
    interchangable internal component.

    Now, in simple situations, it’s possible for a View to guess all the
    defaults for the form — all it needs to know is that you’re dealing
    with a Foo model, and it can construct a default Foo ModelForm.
    However, if you have more sophisticated form requirements, you’re
    going to need a customized Form.

    We could have implemented this by exposing all the options of
    ModelForm on the View class; but in order to keep everything clean, we
    kept the ModelForm isolated, and provided the View with a way to
    specify which Form class it’s going to use.

    So – to cover your use case of excluding fields, you define a
    ModelForm that excludes the fields, then let the CreateView know the
    form you want to use:

    class CampaignForm(forms.ModelForm):
    
    
        class Meta:
            model = Campaign
            exclude = ('user', 'name', 'content_inlined')
    
    class CreateCampaignView(CreateView):
        form_class = CampaignForm
        template_name = "forms/create.html"
    

    I’m guessing when you say “fix a values for a field”, you mean setting
    the values of user, name and content_inlined before you save the new
    Campaign instance; to do this, you need to inject some extra code into
    the form processing logic of the form:

    class CreateCampaignView(CreateView):
        form_class = CampaignForm
        template_name = "forms/create.html"
    
        def form_valid(self, form):
            form.instance.user = ... (something meaningful.. e.g., self.request.user)
            return super(CreateCampaignView, self).form_valid(form)
    

    This overrides the default behavior when the form is valid, and sets
    the extra values. The super() implementation of form_valid() will then
    save the instance.

    For the record, this could also be done by overriding the save()
    method on the ModelForm — however, if you do that, you lose the
    request object, which you will need if you’re trying to set the
    instance values to something that is request-sensitive.

    *the original answer set self.object.user instead of form.instance.user. This gives an AttributeError so I have changed it above.

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

Sidebar

Related Questions

My simplified domain model looks something like this: public abstract class Entity<IdK> { public
I have a such model named Foo: class Foo(models.Model): name = models.CharField() entry =
I have a Domain Model for Countries and States that looks like the following(see
I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
Let's say I have this model named Product with a field named brand .
I have one grails application.In that I have one model class named Book. From
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following situation: A project MyCompany.MyProject.Domain which contains my domain model, and
We have implemented our domain model using the accountability pattern, which we are trying
I have a strong typed view model and a MetaData partial class which has

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.