There are two ways to do this. 

1. You can set the owner as the logged in user by default
2. You can provide all possible owners as options and the user can select 
the owner from a dropdown.

For case 1, you may not want to display the owner field in frontend. Here 
you can override form_valid and set the owner as logged in user.

class CreateDog(CreateView):
    .......
    def form_valid(self, form):
        form.instance.owner = self.request.user
        return super(CreateDog, self).form_valid(form)

For more details:
https://docs.djangoproject.com/en/2.0/topics/class-based-views/generic-editing/#models-and-request-user
http://ccbv.co.uk/projects/Django/1.11/django.views.generic.edit/CreateView/#form_valid


For case 2, you need to populate the list of owners by overriding the 
get_initial method.

class CreateDog(CreateView):
    .......
    def get_initial(self):
        initial_data = super(CreateDog, self).get_initial()
        initial_data["owner"] = [(x.id, x.name) for x in 
Owner.objects.all()]
        return initial_data

For more details:
https://docs.djangoproject.com/en/2.0/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.initial
http://ccbv.co.uk/projects/Django/1.11/django.views.generic.edit/CreateView/#get_initial

On Sunday, 28 January 2018 11:18:28 UTC+5:30, tangoward15 wrote:
>
> When I added the owner field, it doesn't have any value though the user is 
> already registered. 
>
> how do i make it mandatory in createview?
>
>
> owner = Owner.objects.create_user(username="blah", password="blah", email="
> bl...@blah.com <javascript:>")
> dog = Dog(name="mydog", bday=datetime.today(), owner=owner).save()
>
>
> On Sun, Jan 28, 2018 at 1:13 PM, tango ward <tango...@gmail.com 
> <javascript:>> wrote:
>
>> Will try it now. 
>>
>> Quesion about the on_delete=models.CASCADE, does this mean that if I 
>> delete the pet (dog or cat) it will delete the owner who owns them?
>>
>> On Sun, Jan 28, 2018 at 1:11 PM, Akhil Lawrence <akhilp...@gmail.com 
>> <javascript:>> wrote:
>>
>>> Your create view do not accept owner as a input and according to your 
>>> models, owner can be null. That's the problem. Include owner to your create 
>>> view fields as shown below. Also you may consider making owner mandatory as 
>>> mentioned in my previous response.
>>>
>>>
>>> class CreateDog(CreateView):
>>>     model = Dog
>>>     fields = ('name', 'bday', 'owner')
>>>     template_name = 'animals/dog_create.html'
>>>
>>>
>>> class CreateCat(CreateView):
>>>     model = Cat
>>>     fields = ('name', 'bday', 'owner')
>>>     template_name = 'animals/cat_create.html'
>>>
>>> Thanks
>>>
>>> --
>>> You received this message because you are subscribed to the Google 
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to django-users...@googlegroups.com <javascript:>.
>>> To post to this group, send email to django...@googlegroups.com 
>>> <javascript:>.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/340fa5fd-1475-4355-821c-82404b71b6bb%40googlegroups.com
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8ffa55b6-d1fe-453e-8264-5b57f84c9bb8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to