Create new object on ModelForm save, with values from related model

borfast

I have a ModelForm to allow the creation of new User objects via a subclass of CreateView, and I also have a UserProfile model with a "client" field, and connected to the User model. This:

# models.py
class UserProfile(TimeStampedModel):
    user = models.OneToOneField(User, unique=True)
    client = models.ForeignKey(Client)


# forms.py
class UserForm(ModelForm):
    def create_userprofile(self, user, client):
        profile = UserProfile()
        profile.user = user
        profile.client = client
        profile.save()

    class Meta:
        model = User
        fields = ('email', 'username', 'password', 'first_name', 'last_name', 'groups')


# views.py
class UserCreate(LoginRequiredMixin, CreateView):
    model = User
    template_name = 'usermanager/user_form.html'
    form_class = UserForm
    success_url = reverse_lazy('usermanager:list')

    def form_valid(self, form):
        ### Make sure a newly created user has a UserProfile.

        # some pseudo-code thrown in

        # First save the user
        result = super(UserCreate, self).form_valid(form)

        # Now that we have a user, let's create the UserProfile
        form.create_userprofile(created_user, current_user.userprofile.client)

        # Finally return the result of the parent method.
        return result

I want to be able to create a new UserProfile when the form is submitted (and is valid, of course), so I was doing it on the CreateView.form_valid() method, but I need the ID of the just created user, which at that time I don't think I have - do I?

At the same time, I need to assign to the new UserProfile the same client as the current (not the new) user has in his profile.

Any thoughts on how to achieve this?

DRC

try checking if

self.object.pk 

has what you want after calling

super(UserCreate, self).form_valid(form)

in your form_valid method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create model instance using data from related object

From Dev

Parse.com iOS SDK: Create New Related Object on Object Save

From Dev

Best way to create new object from existing object with certain values

From Dev

Waterline: Populate model related object on create (Sails)

From Dev

How can I create a new object from existing values?

From Dev

Create Parent Class object from child class's ModelForm

From Dev

Django model save related model

From Dev

Multiplying all the values from a dataframe by a single column from another dataframe and save the results in a new object

From Dev

How would I use the objects from a related Model to populate the initial value of a Field in a ModelForm?

From Dev

Create new Object to replace values in an immutable Object

From Dev

Create forms from model, save to another

From Dev

Save model object to SQL Server from R

From Dev

Unable to save related object

From Dev

Create a new object in React.js from the values in a separate json object

From Dev

Create Object with Values from Array

From Dev

Rails devise create a related object after new user is created?

From Dev

Create new object from existing array and object

From Dev

django-need to create model object when related forigenkey model object is created

From Dev

Django REST Framework: create related model from POSTed data

From Dev

ModelForm won't create instance of model

From Dev

Django. How to annotate a object count from a related model

From Dev

How do I create a new model, related to an existing model, using STI?

From Dev

Adding Values to Model Array Object from Adapter

From Dev

Save file from ModelForm's FileField

From Dev

Unable to save to database from Django ModelForm

From Dev

Rails how to create a new record of a parent model from the child model

From Dev

Rails - Save item from index and write it to a new Model

From Dev

Create a new color from two RGB values

From Dev

Create new variables from format values

Related Related

  1. 1

    Create model instance using data from related object

  2. 2

    Parse.com iOS SDK: Create New Related Object on Object Save

  3. 3

    Best way to create new object from existing object with certain values

  4. 4

    Waterline: Populate model related object on create (Sails)

  5. 5

    How can I create a new object from existing values?

  6. 6

    Create Parent Class object from child class's ModelForm

  7. 7

    Django model save related model

  8. 8

    Multiplying all the values from a dataframe by a single column from another dataframe and save the results in a new object

  9. 9

    How would I use the objects from a related Model to populate the initial value of a Field in a ModelForm?

  10. 10

    Create new Object to replace values in an immutable Object

  11. 11

    Create forms from model, save to another

  12. 12

    Save model object to SQL Server from R

  13. 13

    Unable to save related object

  14. 14

    Create a new object in React.js from the values in a separate json object

  15. 15

    Create Object with Values from Array

  16. 16

    Rails devise create a related object after new user is created?

  17. 17

    Create new object from existing array and object

  18. 18

    django-need to create model object when related forigenkey model object is created

  19. 19

    Django REST Framework: create related model from POSTed data

  20. 20

    ModelForm won't create instance of model

  21. 21

    Django. How to annotate a object count from a related model

  22. 22

    How do I create a new model, related to an existing model, using STI?

  23. 23

    Adding Values to Model Array Object from Adapter

  24. 24

    Save file from ModelForm's FileField

  25. 25

    Unable to save to database from Django ModelForm

  26. 26

    Rails how to create a new record of a parent model from the child model

  27. 27

    Rails - Save item from index and write it to a new Model

  28. 28

    Create a new color from two RGB values

  29. 29

    Create new variables from format values

HotTag

Archive