Add data manually in CreateView

Hello dear community,
I want to build a profile page for users on my site.
There will be a form for this in which the user can expand his data after registration.

When I run it as described below, the “User” field in the form is rendered as a drop-down menu - which of course is not supposed to be the case.

I could also remove the “User” database field from “fields”. And I get the user via the request (request.user.username). But how can I add data manually in the CreateView - is there a method for this?

view.py

class AddDataToProfileView(CreateView):
    template_name = "user_interface/add_data_profile.html"
    success_url = "/"
    fields = "__all__"
    model = UserProfile

models.py

class UserProfile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    user_image = models.ImageField(upload_to="media")
    about_me = models.TextField()
    interests = models.CharField(max_length = 100)
    society = models.CharField(max_length = 100)
    birthday = models.DateField()
    city = models.CharField(max_length = 100)

You can override the form_valid method in the CreateView to set values for the object being saved. In this situation, it’s probably easier to replace the functionality you need than to worry about calling super within your function.

You’ll want to save the form first, using commit=False. Then you’ll update the object, save the object, and return an HttpResponseRedirect to your success_url.

Also see the second example block at Creating forms from models | Django documentation | Django. (Ignore the references to many-to-many fields - they don’t apply in your case.)

Great - thank you very much - that worked :slight_smile:

Is it also possible to adjust the individual value=“X” (input) in the CreatView within the form?

I can use “get_context_data” to adapt the context in advance and play it in the template. But I can’t find any syntax for this :confused:

I would more or less like to pre-feel the form - with user data.

I’m sorry, I’m not sure I’m following what you’re trying to ask here.

Are you looking to initialize one (or more) form fields for the initial display of the form?

If so, the FormMixin (used by CreateView) allows for a class attribute named initial that you can use to set initial values for the fields in the form.

The form has several fields, if the user has already stored information in the system for some fields, these should be pre-filled - but be customizable.

Let’s say the form had an “About Me” and “Interests” field, now when the user hits this form for the first time, they can leave as much information as they like. Let’s say he defines in the field “Interests” - “Programming”, “About me” he leaves blank. At a later point in time, the same user comes back to the form, now both form fields are displayed to him again, but “Interests” already says “Programming” - the system must check accordingly whether data is stored for one or more fields - if so , these should be pre-filled in the form field, if not - then not.

If I put the following code in the create view (AddDataToProfileView) - I get an empty dictionary. So I don’t know how to integrate information into the form. Do I need a forms.Form or ModelForm for this?

def get_initial(self):
        initial = super().get_initial()
        print(initial)
        return

I’ve searched the documentation for code examples, but haven’t found any examples of how to change the value of a form field.

You don’t change the individual fields. If this is a model form, you create the instance of the form with specifying the instance of the model being represented.

At that point, it’s no longer a CreateView, it’s an UpdateView. CreateView is only used when creating the model instance. Going back later to edit what had been previously saved is an Update.