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 9120889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:43:48+00:00 2026-06-17T05:43:48+00:00

I have a modelform: class UserPreferencesForm(ModelForm): Form for storing user preferences. class Meta: model

  • 0

I have a modelform:

class UserPreferencesForm(ModelForm):
    """
    Form for storing user preferences.
    """
    class Meta:
        model = UserPreferences
        exclude = ('user')

the model:

class UserPreferences(models.Model):
    """
    Model for user project preferences.
    """
    user = models.OneToOneField(User)
    ...

and in the views:

    ...
    form = UserPreferencesForm(request.POST or None)
    if form.is_valid():
        # save the form
        prefs = form.save(commit=False)
        prefs.user = request.user
        prefs.update()
        messages.add_message(
            request, messages.INFO, 'Your preferences have been updated.'
        )
    ...

I want to ensure that each user only has one set of preferences, so I would like to refactor the view code to use something along the lines of the update() model method instead of checking for object existence and then saving, which would incur more queries.

What is the most efficient way of ‘create-or-updating’ the model?

Any help much appreciated.

  • 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-17T05:43:50+00:00Added an answer on June 17, 2026 at 5:43 am

    Are you interested in saving the query to detect if a row exists?

    In the case, you could do as you describe.. do an update and check if 0 rows were updated, which implies the profile doesn’t exist.

    updated = Preferences.objects.filter(user=request.user).update(**form.cleaned_data)
    if updated == 0:
        # create preference object
    

    But an even simpler design pattern is to ensure there is always a preferences table for every user via a signal listening on models.signals.post_save sent by the User class.

    Then, you can assume it always exists.

    def create_prefs(sender, instance, created, **kwargs):
         if created:
             # create prefs
    
    models.signals.post_save.connect(create_prefs, sender=User)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model and it's form: class Project(models.Model) class ProjectForm(forms.ModelForm) class Meta:
I have a sample form: class AdminDiscountForm(ModelForm): class Meta: model = Discount exclude =
I have the following form: class SourceForm(forms.ModelForm): class Meta: model = Source widgets =
I have the following form: class ModuleItemForm2(forms.ModelForm): class Meta: model = Module_item fields =
I have this: class OrderForm(ModelForm): class Meta: model = Order exclude = ('number',) def
I have a class and it's modelForm. class UserGoal(models.Model): user = models.ForeignKey(User) goal =
I have a Modelform: class POwner4NewModel(ModelForm): class Meta: model = ProductOwner exclude = (o_owner,o_owner_desc,o_product_model,o_main_image,o_thumbnail,o_gallery_images,o_timestamp,o_status)
I have a simple model form: class ProductSelectionForm(ModelForm): class Meta: model = Product and
I have a form: class AdForm(ModelForm): class Meta: model = Ad widgets = {
I have this form: class TourForm(ModelForm): class Meta: model = Tour I wanna design

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.