0
class PetAddView(CreateView):
    model = Pet
    fields = ['name','animal_type','breed','color','age','height','price','city','sex','photo']
    template_name = 'pets/pet-add.html'

There's my create view.

My goal is to provide view with functionality to create record in database. Bu I don't need my users to specify slug instead I need to set it automatically from specified "name" value.

Can I?

1 Answer 1

2

Yes, you can do absolutely anything in the form_valid method. The trick is

def form_valid( self, form):

    instance = form.save( commit = False)
    # define the slug and any other programmatically generated fields
    instance.slug = whatever
    instance.save()

    return HttpResponseRedirect( self.get_success_url())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.