Re: CRUD code feedback

2018-01-28 Thread tango ward
Thanks a lot for your help and patience. I really appreciate it!

On Sun, Jan 28, 2018 at 11:32 PM, Akhil Lawrence 
wrote:

> Happy to see that you were able to get it in working condition. Cheers 
>
> On Sunday, 28 January 2018 20:35:48 UTC+5:30, tangoward15 wrote:
>>
>> Holy smoke it's working!!!
>>
>> Thanks a lot!
>>
>> I can use the same pattern for update and delete right?
>>
>> On Sun, Jan 28, 2018 at 10:59 PM, Akhil Lawrence 
>> wrote:
>>
>>> The takeaway from the above error is *AnonymousUser*
>>>
>>>
>>> This means you are not logged into the system. By default 
>>> *self.request.user* will be an instance of *AnonymousUser*,
>>>
>>> the moment you login it becomes *User* instance
>>>
>>>
>>> Make sure you login before hitting this code
>>>
>>>
>>> Thanks.
>>>
>>>
>>> On Sunday, 28 January 2018 20:10:38 UTC+5:30, tangoward15 wrote:
>>>
 I am getting another error:

 Cannot assign ">>> >": 
 "Dog.owner" must be a "User" instance.

 models.py

 from django.db import models
 from django.contrib.auth import get_user_model
 from django.contrib.auth.models import User, PermissionsMixin
 from django.urls import reverse


 # Create your models here.


 class RegUser(User, PermissionsMixin):

 def __str__(self):
 return "@{}".format(self.username)


 class Dog(models.Model):
 name = models.CharField(max_length=40)
 bday = models.DateField()
 owner = models.ForeignKey(User, related_name='dogs',
 on_delete=models.CASCADE)

 def __str__(self):
 return self.name


 views.py


 class CreateDog(CreateView):
 template_name = 'dogs_cats/create_dog.html'
 model = Dog
 fields = ('name', 'bday')

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

 On Sun, Jan 28, 2018 at 10:20 PM, Akhil Lawrence 
 wrote:

> Oops I missed something, you are creating a different table all
> together for *Owner*. In that case you need to insert owner record
> (This is not recommended, since the data is going to be the exact copy of
> User. This will cause data inconsistency, use User instead)
>
> class CreateDog(CreateView):
> template_name = 'dogs_cats/create_dog.html'
> model = Dog
> fields = ('name', 'bday')
>
>
> def form_valid(self, form):
> *owner =
> Owner.objects.create_user(self.request.user.username,
> self.request.user.email, self.request.user.password)*
> *form.instance.owner = owner*
> return super(CreateDog, self).form_valid(form)
>
> But I would say this approach is wrong, your *Owner* is exact copy of
> *User*. Use *User* instead.
>
> The ideal way should be,
>
>
>
>
>
>
>
> *class Cat(models.Model):name = models.CharField(max_length=40)
> bday = models.DateField()owner = models.ForeignKey(User,
> related_name='cats', on_delete=models.CASCADE)*
>
>
>
> *class CreateDog(CreateView):template_name =
> 'dogs_cats/create_dog.html'model = Dog*
> *fields = ('name', 'bday')*
>
>
>
> *def form_valid(self, form):form.instance.owner =
> self.request.userreturn super(CreateDog, self).form_valid(form)*
>
>
> On Sunday, 28 January 2018 19:32:35 UTC+5:30, Akhil Lawrence wrote:
>>
>> This error message is tricky.
>>
>> According to your model definition *owner* is of type *Owner*. This
>> error message actually comes from the *Owner* model.
>>
>> Instead of  *form.instance.owner = self.request.user*, try  
>> *form.instance.owner
>> = Owner(id=self.request.user.id )*
>>
>> It should work.
>>
>> On Sunday, 28 January 2018 18:37:08 UTC+5:30, tangoward15 wrote:
>>>
>>> Sorry, for asking again. In option one, It appears that I am getting
>>> an error for using the owner field which is a ForeignKey.
>>>
>>> "
>>>
>>> ">": "Dog.owner" must be a "User" 
>>> instance."
>>>
>>>
>>>
>>> class CreateDog(CreateView):
>>> template_name = 'dogs_cats/create_dog.html'
>>> model = Dog
>>> fields = ('name', 'bday')
>>>
>>> def form_valid(self, form):
>>> form.instance.owner = self.request.user
>>> return super(CreateDog, self).form_valid(form)
>>>
>>>
>>> I looked at StackOverflow and saw this thread
>>> https://stackoverflow.com/questions/30017334/django-foreign-
>>> key-must-be-an-instance. Is there a way to use the same solution in
>>> side the form_valid()?
>>>
>>>
>>>
>>> On 

Re: CRUD code feedback

2018-01-28 Thread Akhil Lawrence
Happy to see that you were able to get it in working condition. Cheers 

On Sunday, 28 January 2018 20:35:48 UTC+5:30, tangoward15 wrote:
>
> Holy smoke it's working!!!
>
> Thanks a lot!
>
> I can use the same pattern for update and delete right?
>
> On Sun, Jan 28, 2018 at 10:59 PM, Akhil Lawrence  > wrote:
>
>> The takeaway from the above error is *AnonymousUser*
>>
>>
>> This means you are not logged into the system. By default 
>> *self.request.user* will be an instance of *AnonymousUser*,
>>
>> the moment you login it becomes *User* instance
>>
>>
>> Make sure you login before hitting this code
>>
>>
>> Thanks.
>>
>>
>> On Sunday, 28 January 2018 20:10:38 UTC+5:30, tangoward15 wrote:
>>
>>> I am getting another error: 
>>>
>>> Cannot assign ">> object at 0x7fe3fa28fe48>>": "Dog.owner" must be a "User" instance.
>>>
>>> models.py
>>>
>>> from django.db import models
>>> from django.contrib.auth import get_user_model
>>> from django.contrib.auth.models import User, PermissionsMixin
>>> from django.urls import reverse
>>>
>>>
>>> # Create your models here.
>>>
>>>
>>> class RegUser(User, PermissionsMixin):
>>>
>>> def __str__(self):
>>> return "@{}".format(self.username)
>>>
>>>
>>> class Dog(models.Model):
>>> name = models.CharField(max_length=40)
>>> bday = models.DateField()
>>> owner = models.ForeignKey(User, related_name='dogs', 
>>> on_delete=models.CASCADE)
>>>
>>> def __str__(self):
>>> return self.name
>>>
>>>
>>> views.py
>>>
>>>
>>> class CreateDog(CreateView):
>>> template_name = 'dogs_cats/create_dog.html'
>>> model = Dog
>>> fields = ('name', 'bday')
>>>
>>> def form_valid(self, form):
>>> form.instance.owner = self.request.user
>>> return supter(CreateDog, self).form_valid(form)
>>>
>>> On Sun, Jan 28, 2018 at 10:20 PM, Akhil Lawrence  
>>> wrote:
>>>
 Oops I missed something, you are creating a different table all 
 together for *Owner*. In that case you need to insert owner record 
 (This is not recommended, since the data is going to be the exact copy of 
 User. This will cause data inconsistency, use User instead)

 class CreateDog(CreateView):
 template_name = 'dogs_cats/create_dog.html'
 model = Dog
 fields = ('name', 'bday')


 def form_valid(self, form):
 *owner = Owner.objects.create_user(self.request.user.username, 
 self.request.user.email, self.request.user.password)*
 *form.instance.owner = owner*
 return super(CreateDog, self).form_valid(form)

 But I would say this approach is wrong, your *Owner* is exact copy of 
 *User*. Use *User* instead.

 The ideal way should be,







 *class Cat(models.Model):name = models.CharField(max_length=40)
 bday = models.DateField()owner = models.ForeignKey(User, 
 related_name='cats', on_delete=models.CASCADE)*



 *class CreateDog(CreateView):template_name = 
 'dogs_cats/create_dog.html'model = Dog*
 *fields = ('name', 'bday')*



 *def form_valid(self, form):form.instance.owner = 
 self.request.userreturn super(CreateDog, self).form_valid(form)*


 On Sunday, 28 January 2018 19:32:35 UTC+5:30, Akhil Lawrence wrote:
>
> This error message is tricky.
>
> According to your model definition *owner* is of type *Owner*. This 
> error message actually comes from the *Owner* model.
>
> Instead of  *form.instance.owner = self.request.user*, try  
> *form.instance.owner 
> = Owner(id=self.request.user.id )*
>
> It should work.
>
> On Sunday, 28 January 2018 18:37:08 UTC+5:30, tangoward15 wrote:
>>
>> Sorry, for asking again. In option one, It appears that I am getting 
>> an error for using the owner field which is a ForeignKey. 
>>
>> "
>>
>> ">": "Dog.owner" must be a "User" 
>> instance."
>>
>>
>>
>> class CreateDog(CreateView):
>> template_name = 'dogs_cats/create_dog.html'
>> model = Dog
>> fields = ('name', 'bday')
>>
>> def form_valid(self, form):
>> form.instance.owner = self.request.user
>> return super(CreateDog, self).form_valid(form)
>>
>>
>> I looked at StackOverflow and saw this thread 
>> https://stackoverflow.com/questions/30017334/django-foreign-key-must-be-an-instance.
>>  
>> Is there a way to use the same solution in side the form_valid()?
>>
>>
>>
>> On Sun, Jan 28, 2018 at 5:32 PM, tango ward  
>> wrote:
>>
>>> Thanks, I'm digesting it. I am going to use the first option. 
>>>
>>> On Sun, Jan 28, 2018 at 3:20 PM, Akhil Lawrence >> > wrote:
>>>
 I 

Re: CRUD code feedback

2018-01-28 Thread tango ward
Holy smoke it's working!!!

Thanks a lot!

I can use the same pattern for update and delete right?

On Sun, Jan 28, 2018 at 10:59 PM, Akhil Lawrence 
wrote:

> The takeaway from the above error is *AnonymousUser*
>
>
> This means you are not logged into the system. By default *self.request.user* 
> will be an instance of *AnonymousUser*,
>
> the moment you login it becomes *User* instance
>
>
> Make sure you login before hitting this code
>
>
> Thanks.
>
>
> On Sunday, 28 January 2018 20:10:38 UTC+5:30, tangoward15 wrote:
>
>> I am getting another error:
>>
>> Cannot assign "> object at 0x7fe3fa28fe48>>": "Dog.owner" must be a "User" instance.
>>
>> models.py
>>
>> from django.db import models
>> from django.contrib.auth import get_user_model
>> from django.contrib.auth.models import User, PermissionsMixin
>> from django.urls import reverse
>>
>>
>> # Create your models here.
>>
>>
>> class RegUser(User, PermissionsMixin):
>>
>> def __str__(self):
>> return "@{}".format(self.username)
>>
>>
>> class Dog(models.Model):
>> name = models.CharField(max_length=40)
>> bday = models.DateField()
>> owner = models.ForeignKey(User, related_name='dogs',
>> on_delete=models.CASCADE)
>>
>> def __str__(self):
>> return self.name
>>
>>
>> views.py
>>
>>
>> class CreateDog(CreateView):
>> template_name = 'dogs_cats/create_dog.html'
>> model = Dog
>> fields = ('name', 'bday')
>>
>> def form_valid(self, form):
>> form.instance.owner = self.request.user
>> return supter(CreateDog, self).form_valid(form)
>>
>> On Sun, Jan 28, 2018 at 10:20 PM, Akhil Lawrence 
>> wrote:
>>
>>> Oops I missed something, you are creating a different table all together
>>> for *Owner*. In that case you need to insert owner record (This is not
>>> recommended, since the data is going to be the exact copy of User. This
>>> will cause data inconsistency, use User instead)
>>>
>>> class CreateDog(CreateView):
>>> template_name = 'dogs_cats/create_dog.html'
>>> model = Dog
>>> fields = ('name', 'bday')
>>>
>>>
>>> def form_valid(self, form):
>>> *owner = Owner.objects.create_user(self.request.user.username,
>>> self.request.user.email, self.request.user.password)*
>>> *form.instance.owner = owner*
>>> return super(CreateDog, self).form_valid(form)
>>>
>>> But I would say this approach is wrong, your *Owner* is exact copy of
>>> *User*. Use *User* instead.
>>>
>>> The ideal way should be,
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> *class Cat(models.Model):name = models.CharField(max_length=40)
>>> bday = models.DateField()owner = models.ForeignKey(User,
>>> related_name='cats', on_delete=models.CASCADE)*
>>>
>>>
>>>
>>> *class CreateDog(CreateView):template_name =
>>> 'dogs_cats/create_dog.html'model = Dog*
>>> *fields = ('name', 'bday')*
>>>
>>>
>>>
>>> *def form_valid(self, form):form.instance.owner =
>>> self.request.userreturn super(CreateDog, self).form_valid(form)*
>>>
>>>
>>> On Sunday, 28 January 2018 19:32:35 UTC+5:30, Akhil Lawrence wrote:

 This error message is tricky.

 According to your model definition *owner* is of type *Owner*. This
 error message actually comes from the *Owner* model.

 Instead of  *form.instance.owner = self.request.user*, try  
 *form.instance.owner
 = Owner(id=self.request.user.id )*

 It should work.

 On Sunday, 28 January 2018 18:37:08 UTC+5:30, tangoward15 wrote:
>
> Sorry, for asking again. In option one, It appears that I am getting
> an error for using the owner field which is a ForeignKey.
>
> "
>
> ">": "Dog.owner" must be a "User" 
> instance."
>
>
>
> class CreateDog(CreateView):
> template_name = 'dogs_cats/create_dog.html'
> model = Dog
> fields = ('name', 'bday')
>
> def form_valid(self, form):
> form.instance.owner = self.request.user
> return super(CreateDog, self).form_valid(form)
>
>
> I looked at StackOverflow and saw this thread
> https://stackoverflow.com/questions/30017334/django-foreign-
> key-must-be-an-instance. Is there a way to use the same solution in
> side the form_valid()?
>
>
>
> On Sun, Jan 28, 2018 at 5:32 PM, tango ward 
> wrote:
>
>> Thanks, I'm digesting it. I am going to use the first option.
>>
>> On Sun, Jan 28, 2018 at 3:20 PM, Akhil Lawrence 
>> wrote:
>>
>>> I doubt whether you are reading my response properly. Let me
>>> reiterate my response for you.
>>>
>>>
>>> *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 

Re: CRUD code feedback

2018-01-28 Thread Akhil Lawrence


The takeaway from the above error is *AnonymousUser*


This means you are not logged into the system. By default *self.request.user* 
will be an instance of *AnonymousUser*,

the moment you login it becomes *User* instance


Make sure you login before hitting this code


Thanks.


On Sunday, 28 January 2018 20:10:38 UTC+5:30, tangoward15 wrote:
>
> I am getting another error: 
>
> Cannot assign " object at 0x7fe3fa28fe48>>": "Dog.owner" must be a "User" instance.
>
> models.py
>
> from django.db import models
> from django.contrib.auth import get_user_model
> from django.contrib.auth.models import User, PermissionsMixin
> from django.urls import reverse
>
>
> # Create your models here.
>
>
> class RegUser(User, PermissionsMixin):
>
> def __str__(self):
> return "@{}".format(self.username)
>
>
> class Dog(models.Model):
> name = models.CharField(max_length=40)
> bday = models.DateField()
> owner = models.ForeignKey(User, related_name='dogs', 
> on_delete=models.CASCADE)
>
> def __str__(self):
> return self.name
>
>
> views.py
>
>
> class CreateDog(CreateView):
> template_name = 'dogs_cats/create_dog.html'
> model = Dog
> fields = ('name', 'bday')
>
> def form_valid(self, form):
> form.instance.owner = self.request.user
> return supter(CreateDog, self).form_valid(form)
>
> On Sun, Jan 28, 2018 at 10:20 PM, Akhil Lawrence  > wrote:
>
>> Oops I missed something, you are creating a different table all together 
>> for *Owner*. In that case you need to insert owner record (This is not 
>> recommended, since the data is going to be the exact copy of User. This 
>> will cause data inconsistency, use User instead)
>>
>> class CreateDog(CreateView):
>> template_name = 'dogs_cats/create_dog.html'
>> model = Dog
>> fields = ('name', 'bday')
>>
>>
>> def form_valid(self, form):
>> *owner = Owner.objects.create_user(self.request.user.username, 
>> self.request.user.email, self.request.user.password)*
>> *form.instance.owner = owner*
>> return super(CreateDog, self).form_valid(form)
>>
>> But I would say this approach is wrong, your *Owner* is exact copy of 
>> *User*. Use *User* instead.
>>
>> The ideal way should be,
>>
>>
>>
>>
>>
>>
>>
>> *class Cat(models.Model):name = models.CharField(max_length=40)
>> bday = models.DateField()owner = models.ForeignKey(User, 
>> related_name='cats', on_delete=models.CASCADE)*
>>
>>
>>
>> *class CreateDog(CreateView):template_name = 
>> 'dogs_cats/create_dog.html'model = Dog*
>> *fields = ('name', 'bday')*
>>
>>
>>
>> *def form_valid(self, form):form.instance.owner = 
>> self.request.userreturn super(CreateDog, self).form_valid(form)*
>>
>>
>> On Sunday, 28 January 2018 19:32:35 UTC+5:30, Akhil Lawrence wrote:
>>>
>>> This error message is tricky.
>>>
>>> According to your model definition *owner* is of type *Owner*. This 
>>> error message actually comes from the *Owner* model.
>>>
>>> Instead of  *form.instance.owner = self.request.user*, try  
>>> *form.instance.owner 
>>> = Owner(id=self.request.user.id )*
>>>
>>> It should work.
>>>
>>> On Sunday, 28 January 2018 18:37:08 UTC+5:30, tangoward15 wrote:

 Sorry, for asking again. In option one, It appears that I am getting an 
 error for using the owner field which is a ForeignKey. 

 "

 ">": "Dog.owner" must be a "User" 
 instance."



 class CreateDog(CreateView):
 template_name = 'dogs_cats/create_dog.html'
 model = Dog
 fields = ('name', 'bday')

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


 I looked at StackOverflow and saw this thread 
 https://stackoverflow.com/questions/30017334/django-foreign-key-must-be-an-instance.
  
 Is there a way to use the same solution in side the form_valid()?



 On Sun, Jan 28, 2018 at 5:32 PM, tango ward  wrote:

> Thanks, I'm digesting it. I am going to use the first option. 
>
> On Sun, Jan 28, 2018 at 3:20 PM, Akhil Lawrence  
> wrote:
>
>> I doubt whether you are reading my response properly. Let me 
>> reiterate my response for you.
>>
>>
>> *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 = 

Re: CRUD code feedback

2018-01-28 Thread tango ward
I am getting another error:

Cannot assign ">":
"Dog.owner" must be a "User" instance.

models.py

from django.db import models
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User, PermissionsMixin
from django.urls import reverse


# Create your models here.


class RegUser(User, PermissionsMixin):

def __str__(self):
return "@{}".format(self.username)


class Dog(models.Model):
name = models.CharField(max_length=40)
bday = models.DateField()
owner = models.ForeignKey(User, related_name='dogs',
on_delete=models.CASCADE)

def __str__(self):
return self.name


views.py


class CreateDog(CreateView):
template_name = 'dogs_cats/create_dog.html'
model = Dog
fields = ('name', 'bday')

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

On Sun, Jan 28, 2018 at 10:20 PM, Akhil Lawrence 
wrote:

> Oops I missed something, you are creating a different table all together
> for *Owner*. In that case you need to insert owner record (This is not
> recommended, since the data is going to be the exact copy of User. This
> will cause data inconsistency, use User instead)
>
> class CreateDog(CreateView):
> template_name = 'dogs_cats/create_dog.html'
> model = Dog
> fields = ('name', 'bday')
>
>
> def form_valid(self, form):
> *owner = Owner.objects.create_user(self.request.user.username,
> self.request.user.email, self.request.user.password)*
> *form.instance.owner = owner*
> return super(CreateDog, self).form_valid(form)
>
> But I would say this approach is wrong, your *Owner* is exact copy of
> *User*. Use *User* instead.
>
> The ideal way should be,
>
>
>
>
>
>
>
> *class Cat(models.Model):name = models.CharField(max_length=40)
> bday = models.DateField()owner = models.ForeignKey(User,
> related_name='cats', on_delete=models.CASCADE)*
>
>
>
> *class CreateDog(CreateView):template_name =
> 'dogs_cats/create_dog.html'model = Dog*
> *fields = ('name', 'bday')*
>
>
>
> *def form_valid(self, form):form.instance.owner =
> self.request.userreturn super(CreateDog, self).form_valid(form)*
>
>
> On Sunday, 28 January 2018 19:32:35 UTC+5:30, Akhil Lawrence wrote:
>>
>> This error message is tricky.
>>
>> According to your model definition *owner* is of type *Owner*. This
>> error message actually comes from the *Owner* model.
>>
>> Instead of  *form.instance.owner = self.request.user*, try  
>> *form.instance.owner
>> = Owner(id=self.request.user.id )*
>>
>> It should work.
>>
>> On Sunday, 28 January 2018 18:37:08 UTC+5:30, tangoward15 wrote:
>>>
>>> Sorry, for asking again. In option one, It appears that I am getting an
>>> error for using the owner field which is a ForeignKey.
>>>
>>> "
>>>
>>> ">": "Dog.owner" must be a "User" 
>>> instance."
>>>
>>>
>>>
>>> class CreateDog(CreateView):
>>> template_name = 'dogs_cats/create_dog.html'
>>> model = Dog
>>> fields = ('name', 'bday')
>>>
>>> def form_valid(self, form):
>>> form.instance.owner = self.request.user
>>> return super(CreateDog, self).form_valid(form)
>>>
>>>
>>> I looked at StackOverflow and saw this thread
>>> https://stackoverflow.com/questions/30017334/django-foreign-
>>> key-must-be-an-instance. Is there a way to use the same solution in
>>> side the form_valid()?
>>>
>>>
>>>
>>> On Sun, Jan 28, 2018 at 5:32 PM, tango ward  wrote:
>>>
 Thanks, I'm digesting it. I am going to use the first option.

 On Sun, Jan 28, 2018 at 3:20 PM, Akhil Lawrence 
 wrote:

> I doubt whether you are reading my response properly. Let me reiterate
> my response for you.
>
>
> *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 

Re: CRUD code feedback

2018-01-28 Thread Akhil Lawrence
Oops I missed something, you are creating a different table all together 
for *Owner*. In that case you need to insert owner record (This is not 
recommended, since the data is going to be the exact copy of User. This 
will cause data inconsistency, use User instead)

class CreateDog(CreateView):
template_name = 'dogs_cats/create_dog.html'
model = Dog
fields = ('name', 'bday')


def form_valid(self, form):
*owner = Owner.objects.create_user(self.request.user.username, 
self.request.user.email, self.request.user.password)*
*form.instance.owner = owner*
return super(CreateDog, self).form_valid(form)

But I would say this approach is wrong, your *Owner* is exact copy of *User*. 
Use *User* instead.

The ideal way should be,







*class Cat(models.Model):name = models.CharField(max_length=40)bday 
= models.DateField()owner = models.ForeignKey(User, 
related_name='cats', on_delete=models.CASCADE)*



*class CreateDog(CreateView):template_name = 
'dogs_cats/create_dog.html'model = Dog*
*fields = ('name', 'bday')*



*def form_valid(self, form):form.instance.owner = 
self.request.userreturn super(CreateDog, self).form_valid(form)*


On Sunday, 28 January 2018 19:32:35 UTC+5:30, Akhil Lawrence wrote:
>
> This error message is tricky.
>
> According to your model definition *owner* is of type *Owner*. This error 
> message actually comes from the *Owner* model.
>
> Instead of  *form.instance.owner = self.request.user*, try  
> *form.instance.owner 
> = Owner(id=self.request.user.id )*
>
> It should work.
>
> On Sunday, 28 January 2018 18:37:08 UTC+5:30, tangoward15 wrote:
>>
>> Sorry, for asking again. In option one, It appears that I am getting an 
>> error for using the owner field which is a ForeignKey. 
>>
>> "
>>
>> ">": "Dog.owner" must be a "User" instance."
>>
>>
>>
>> class CreateDog(CreateView):
>> template_name = 'dogs_cats/create_dog.html'
>> model = Dog
>> fields = ('name', 'bday')
>>
>> def form_valid(self, form):
>> form.instance.owner = self.request.user
>> return super(CreateDog, self).form_valid(form)
>>
>>
>> I looked at StackOverflow and saw this thread 
>> https://stackoverflow.com/questions/30017334/django-foreign-key-must-be-an-instance.
>>  
>> Is there a way to use the same solution in side the form_valid()?
>>
>>
>>
>> On Sun, Jan 28, 2018 at 5:32 PM, tango ward  wrote:
>>
>>> Thanks, I'm digesting it. I am going to use the first option. 
>>>
>>> On Sun, Jan 28, 2018 at 3:20 PM, Akhil Lawrence  
>>> wrote:
>>>
 I doubt whether you are reading my response properly. Let me reiterate 
 my response for you.


 *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*
  
 


 if you don't want to show the owner and want to associate the pet with 
 owner via backend go for option1 (also note that you may need to remove 
 the 
 owner from fields of your createview)
 if you 

Re: CRUD code feedback

2018-01-28 Thread tango ward
Now I am getting this error

"save() prohibited to prevent data loss due to unsaved related object
'owner'."


Really appreciate you're help/suggestions. My first time to mess
around with CRUD with user registration, login, logout.


On Sun, Jan 28, 2018 at 10:02 PM, Akhil Lawrence 
wrote:

> This error message is tricky.
>
> According to your model definition *owner* is of type *Owner*. This error
> message actually comes from the *Owner* model.
>
> Instead of  *form.instance.owner = self.request.user*, try  
> *form.instance.owner
> = Owner(id=self.request.user.id )*
>
> It should work.
>
> On Sunday, 28 January 2018 18:37:08 UTC+5:30, tangoward15 wrote:
>>
>> Sorry, for asking again. In option one, It appears that I am getting an
>> error for using the owner field which is a ForeignKey.
>>
>> "
>>
>> ">": "Dog.owner" must be a "User" instance."
>>
>>
>>
>> class CreateDog(CreateView):
>> template_name = 'dogs_cats/create_dog.html'
>> model = Dog
>> fields = ('name', 'bday')
>>
>> def form_valid(self, form):
>> form.instance.owner = self.request.user
>> return super(CreateDog, self).form_valid(form)
>>
>>
>> I looked at StackOverflow and saw this thread
>> https://stackoverflow.com/questions/30017334/django-foreign-
>> key-must-be-an-instance. Is there a way to use the same solution in side
>> the form_valid()?
>>
>>
>>
>> On Sun, Jan 28, 2018 at 5:32 PM, tango ward  wrote:
>>
>>> Thanks, I'm digesting it. I am going to use the first option.
>>>
>>> On Sun, Jan 28, 2018 at 3:20 PM, Akhil Lawrence 
>>> wrote:
>>>
 I doubt whether you are reading my response properly. Let me reiterate
 my response for you.


 *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*
 


 if you don't want to show the owner and want to associate the pet with
 owner via backend go for option1 (also note that you may need to remove the
 owner from fields of your createview)
 if you want to show a dropdown of possible owners go for option 2.

 If you need further assistance, please share your screen via chrome
 remote desktop or something and lets solve it

 Thanks.


 On Sunday, 28 January 2018 12:29:58 UTC+5:30, tangoward15 wrote:
>
> Sorry for being a drag, I tried this code:
>
> class CreateDog(CreateView):
> template_name = 'dogs_cats/create_dog.html'
> model = Dog
> fields = ('name', 'bday', 'owner')
>
> def get_initial(self):
> initial_data = super(CreateDog, self).get_initial()
> initial_data["owner"] = [(self.request.user.id,
> self.request.user.username)]
> return initial_data
>
>
> still I am getting the Admin user in the drop down list for owner.
>
> On Sun, Jan 28, 2018 at 2:31 PM, Akhil Lawrence 
> wrote:
>
>>
>> override get_initial and set only the logged in user
>>
>> class CreateDog(CreateView):

Re: CRUD code feedback

2018-01-28 Thread Akhil Lawrence
This error message is tricky.

According to your model definition *owner* is of type *Owner*. This error 
message actually comes from the *Owner* model.

Instead of  *form.instance.owner = self.request.user*, try  
*form.instance.owner 
= Owner(id=self.request.user.id)*

It should work.

On Sunday, 28 January 2018 18:37:08 UTC+5:30, tangoward15 wrote:
>
> Sorry, for asking again. In option one, It appears that I am getting an 
> error for using the owner field which is a ForeignKey. 
>
> "
>
> ">": "Dog.owner" must be a "User" instance."
>
>
>
> class CreateDog(CreateView):
> template_name = 'dogs_cats/create_dog.html'
> model = Dog
> fields = ('name', 'bday')
>
> def form_valid(self, form):
> form.instance.owner = self.request.user
> return super(CreateDog, self).form_valid(form)
>
>
> I looked at StackOverflow and saw this thread 
> https://stackoverflow.com/questions/30017334/django-foreign-key-must-be-an-instance.
>  
> Is there a way to use the same solution in side the form_valid()?
>
>
>
> On Sun, Jan 28, 2018 at 5:32 PM, tango ward  > wrote:
>
>> Thanks, I'm digesting it. I am going to use the first option. 
>>
>> On Sun, Jan 28, 2018 at 3:20 PM, Akhil Lawrence > > wrote:
>>
>>> I doubt whether you are reading my response properly. Let me reiterate 
>>> my response for you.
>>>
>>>
>>> *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*
>>>  
>>> 
>>>
>>>
>>> if you don't want to show the owner and want to associate the pet with 
>>> owner via backend go for option1 (also note that you may need to remove the 
>>> owner from fields of your createview)
>>> if you want to show a dropdown of possible owners go for option 2.
>>>
>>> If you need further assistance, please share your screen via chrome 
>>> remote desktop or something and lets solve it
>>>
>>> Thanks.
>>>
>>>
>>> On Sunday, 28 January 2018 12:29:58 UTC+5:30, tangoward15 wrote:

 Sorry for being a drag, I tried this code:

 class CreateDog(CreateView):
 template_name = 'dogs_cats/create_dog.html'
 model = Dog
 fields = ('name', 'bday', 'owner')

 def get_initial(self):
 initial_data = super(CreateDog, self).get_initial()
 initial_data["owner"] = [(self.request.user.id, 
 self.request.user.username)]
 return initial_data


 still I am getting the Admin user in the drop down list for owner.

 On Sun, Jan 28, 2018 at 2:31 PM, Akhil Lawrence  
 wrote:

>
> override get_initial and set only the logged in user
>
> class CreateDog(CreateView):
> ...
> def get_initial(self):
> initial_data = super(CreateDog, self).get_initial()
> initial_data["owner"] = [(self.request.user.id, 
> self.request.user.username)]
> return initial_data
>
>
>
>
> On Sunday, 28 January 2018 11:52:31 UTC+5:30, tangoward15 wrote:
>>
>> I updated my models. py
>>
>> from django.db import models
>> from 

Re: CRUD code feedback

2018-01-28 Thread tango ward
Sorry, for asking again. In option one, It appears that I am getting an
error for using the owner field which is a ForeignKey.

"

">": "Dog.owner" must be a "User" instance."



class CreateDog(CreateView):
template_name = 'dogs_cats/create_dog.html'
model = Dog
fields = ('name', 'bday')

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


I looked at StackOverflow and saw this thread
https://stackoverflow.com/questions/30017334/django-foreign-key-must-be-an-instance.
Is there a way to use the same solution in side the form_valid()?



On Sun, Jan 28, 2018 at 5:32 PM, tango ward  wrote:

> Thanks, I'm digesting it. I am going to use the first option.
>
> On Sun, Jan 28, 2018 at 3:20 PM, Akhil Lawrence 
> wrote:
>
>> I doubt whether you are reading my response properly. Let me reiterate my
>> response for you.
>>
>>
>> *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*
>> 
>>
>>
>> if you don't want to show the owner and want to associate the pet with
>> owner via backend go for option1 (also note that you may need to remove the
>> owner from fields of your createview)
>> if you want to show a dropdown of possible owners go for option 2.
>>
>> If you need further assistance, please share your screen via chrome
>> remote desktop or something and lets solve it
>>
>> Thanks.
>>
>>
>> On Sunday, 28 January 2018 12:29:58 UTC+5:30, tangoward15 wrote:
>>>
>>> Sorry for being a drag, I tried this code:
>>>
>>> class CreateDog(CreateView):
>>> template_name = 'dogs_cats/create_dog.html'
>>> model = Dog
>>> fields = ('name', 'bday', 'owner')
>>>
>>> def get_initial(self):
>>> initial_data = super(CreateDog, self).get_initial()
>>> initial_data["owner"] = [(self.request.user.id,
>>> self.request.user.username)]
>>> return initial_data
>>>
>>>
>>> still I am getting the Admin user in the drop down list for owner.
>>>
>>> On Sun, Jan 28, 2018 at 2:31 PM, Akhil Lawrence 
>>> wrote:
>>>

 override get_initial and set only the logged in user

 class CreateDog(CreateView):
 ...
 def get_initial(self):
 initial_data = super(CreateDog, self).get_initial()
 initial_data["owner"] = [(self.request.user.id,
 self.request.user.username)]
 return initial_data




 On Sunday, 28 January 2018 11:52:31 UTC+5:30, tangoward15 wrote:
>
> I updated my models. py
>
> from django.db import models
> from django.contrib.auth import get_user_model
> from django.contrib.auth.models import User, PermissionsMixin
>
> # Create your models here.
>
>
> Pet_Owner = get_user_model()
>
>
> class RegisteredUser(User, PermissionsMixin):
>
> def __str__(self):
> return self.username
>
>
> class Dog(models.Model):
> name = models.CharField(max_length=40)
> bday = models.DateField()
> owner = models.ForeignKey(Pet_Owner, related_name='dogs',
> 

Re: CRUD code feedback

2018-01-28 Thread tango ward
Thanks, I'm digesting it. I am going to use the first option.

On Sun, Jan 28, 2018 at 3:20 PM, Akhil Lawrence 
wrote:

> I doubt whether you are reading my response properly. Let me reiterate my
> response for you.
>
>
> *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*
> 
>
>
> if you don't want to show the owner and want to associate the pet with
> owner via backend go for option1 (also note that you may need to remove the
> owner from fields of your createview)
> if you want to show a dropdown of possible owners go for option 2.
>
> If you need further assistance, please share your screen via chrome remote
> desktop or something and lets solve it
>
> Thanks.
>
>
> On Sunday, 28 January 2018 12:29:58 UTC+5:30, tangoward15 wrote:
>>
>> Sorry for being a drag, I tried this code:
>>
>> class CreateDog(CreateView):
>> template_name = 'dogs_cats/create_dog.html'
>> model = Dog
>> fields = ('name', 'bday', 'owner')
>>
>> def get_initial(self):
>> initial_data = super(CreateDog, self).get_initial()
>> initial_data["owner"] = [(self.request.user.id,
>> self.request.user.username)]
>> return initial_data
>>
>>
>> still I am getting the Admin user in the drop down list for owner.
>>
>> On Sun, Jan 28, 2018 at 2:31 PM, Akhil Lawrence 
>> wrote:
>>
>>>
>>> override get_initial and set only the logged in user
>>>
>>> class CreateDog(CreateView):
>>> ...
>>> def get_initial(self):
>>> initial_data = super(CreateDog, self).get_initial()
>>> initial_data["owner"] = [(self.request.user.id,
>>> self.request.user.username)]
>>> return initial_data
>>>
>>>
>>>
>>>
>>> On Sunday, 28 January 2018 11:52:31 UTC+5:30, tangoward15 wrote:

 I updated my models. py

 from django.db import models
 from django.contrib.auth import get_user_model
 from django.contrib.auth.models import User, PermissionsMixin

 # Create your models here.


 Pet_Owner = get_user_model()


 class RegisteredUser(User, PermissionsMixin):

 def __str__(self):
 return self.username


 class Dog(models.Model):
 name = models.CharField(max_length=40)
 bday = models.DateField()
 owner = models.ForeignKey(Pet_Owner, related_name='dogs',
 on_delete=models.CASCADE)

 def __str__(self):
 return self.name + ' - ' + str(self.owner)


 class Cat(models.Model):
 name = models.CharField(max_length=40)
 bday = models.DateField()
 owner = models.ForeignKey(Pet_Owner, related_name='cats',
 on_delete=models.CASCADE)

 def __str__(self):
 return self.name + ' - ' + str(self.owner)


 I can see all users as drop down list for owner field using Pet_Owner =
 get_user_model() .Problem is, it shows all the Users in the project not the
 only one who is currently logged in.

 On Sun, Jan 28, 2018 at 2:06 PM, Akhil Lawrence 
 wrote:

> Its the reverse. If the owner is deleted, all the cats and dogs
> associated with the owner will be deleted.
>

Re: CRUD code feedback

2018-01-27 Thread Akhil Lawrence
I doubt whether you are reading my response properly. Let me reiterate my 
response for you.


*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*
 



if you don't want to show the owner and want to associate the pet with 
owner via backend go for option1 (also note that you may need to remove the 
owner from fields of your createview)
if you want to show a dropdown of possible owners go for option 2.

If you need further assistance, please share your screen via chrome remote 
desktop or something and lets solve it

Thanks.


On Sunday, 28 January 2018 12:29:58 UTC+5:30, tangoward15 wrote:
>
> Sorry for being a drag, I tried this code:
>
> class CreateDog(CreateView):
> template_name = 'dogs_cats/create_dog.html'
> model = Dog
> fields = ('name', 'bday', 'owner')
>
> def get_initial(self):
> initial_data = super(CreateDog, self).get_initial()
> initial_data["owner"] = [(self.request.user.id, 
> self.request.user.username)]
> return initial_data
>
>
> still I am getting the Admin user in the drop down list for owner.
>
> On Sun, Jan 28, 2018 at 2:31 PM, Akhil Lawrence  > wrote:
>
>>
>> override get_initial and set only the logged in user
>>
>> class CreateDog(CreateView):
>> ...
>> def get_initial(self):
>> initial_data = super(CreateDog, self).get_initial()
>> initial_data["owner"] = [(self.request.user.id, 
>> self.request.user.username)]
>> return initial_data
>>
>>
>>
>>
>> On Sunday, 28 January 2018 11:52:31 UTC+5:30, tangoward15 wrote:
>>>
>>> I updated my models. py
>>>
>>> from django.db import models
>>> from django.contrib.auth import get_user_model
>>> from django.contrib.auth.models import User, PermissionsMixin
>>>
>>> # Create your models here.
>>>
>>>
>>> Pet_Owner = get_user_model()
>>>
>>>
>>> class RegisteredUser(User, PermissionsMixin):
>>>
>>> def __str__(self):
>>> return self.username
>>>
>>>
>>> class Dog(models.Model):
>>> name = models.CharField(max_length=40)
>>> bday = models.DateField()
>>> owner = models.ForeignKey(Pet_Owner, related_name='dogs', 
>>> on_delete=models.CASCADE)
>>>
>>> def __str__(self):
>>> return self.name + ' - ' + str(self.owner)
>>>
>>>
>>> class Cat(models.Model):
>>> name = models.CharField(max_length=40)
>>> bday = models.DateField()
>>> owner = models.ForeignKey(Pet_Owner, related_name='cats', 
>>> on_delete=models.CASCADE)
>>>
>>> def __str__(self):
>>> return self.name + ' - ' + str(self.owner)
>>>
>>>
>>> I can see all users as drop down list for owner field using Pet_Owner = 
>>> get_user_model() .Problem is, it shows all the Users in the project not the 
>>> only one who is currently logged in.
>>>
>>> On Sun, Jan 28, 2018 at 2:06 PM, Akhil Lawrence  
>>> wrote:
>>>
 Its the reverse. If the owner is deleted, all the cats and dogs 
 associated with the owner will be deleted.

 On Sunday, 28 January 2018 10:43:54 UTC+5:30, tangoward15 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, 

Re: CRUD code feedback

2018-01-27 Thread tango ward
Sorry for being a drag, I tried this code:

class CreateDog(CreateView):
template_name = 'dogs_cats/create_dog.html'
model = Dog
fields = ('name', 'bday', 'owner')

def get_initial(self):
initial_data = super(CreateDog, self).get_initial()
initial_data["owner"] = [(self.request.user.id,
self.request.user.username)]
return initial_data


still I am getting the Admin user in the drop down list for owner.

On Sun, Jan 28, 2018 at 2:31 PM, Akhil Lawrence 
wrote:

>
> override get_initial and set only the logged in user
>
> class CreateDog(CreateView):
> ...
> def get_initial(self):
> initial_data = super(CreateDog, self).get_initial()
> initial_data["owner"] = [(self.request.user.id,
> self.request.user.username)]
> return initial_data
>
>
>
>
> On Sunday, 28 January 2018 11:52:31 UTC+5:30, tangoward15 wrote:
>>
>> I updated my models. py
>>
>> from django.db import models
>> from django.contrib.auth import get_user_model
>> from django.contrib.auth.models import User, PermissionsMixin
>>
>> # Create your models here.
>>
>>
>> Pet_Owner = get_user_model()
>>
>>
>> class RegisteredUser(User, PermissionsMixin):
>>
>> def __str__(self):
>> return self.username
>>
>>
>> class Dog(models.Model):
>> name = models.CharField(max_length=40)
>> bday = models.DateField()
>> owner = models.ForeignKey(Pet_Owner, related_name='dogs',
>> on_delete=models.CASCADE)
>>
>> def __str__(self):
>> return self.name + ' - ' + str(self.owner)
>>
>>
>> class Cat(models.Model):
>> name = models.CharField(max_length=40)
>> bday = models.DateField()
>> owner = models.ForeignKey(Pet_Owner, related_name='cats',
>> on_delete=models.CASCADE)
>>
>> def __str__(self):
>> return self.name + ' - ' + str(self.owner)
>>
>>
>> I can see all users as drop down list for owner field using Pet_Owner =
>> get_user_model() .Problem is, it shows all the Users in the project not the
>> only one who is currently logged in.
>>
>> On Sun, Jan 28, 2018 at 2:06 PM, Akhil Lawrence 
>> wrote:
>>
>>> Its the reverse. If the owner is deleted, all the cats and dogs
>>> associated with the owner will be deleted.
>>>
>>> On Sunday, 28 January 2018 10:43:54 UTC+5:30, tangoward15 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 
 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.
> To post to this group, send email to django...@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/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...@googlegroups.com.
>>> To post to this group, send email to django...@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/ms
>>> gid/django-users/09d7c9ca-7dab-4787-94d9-09b3a1bc682a%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 

Re: CRUD code feedback

2018-01-27 Thread Akhil Lawrence

override get_initial and set only the logged in user

class CreateDog(CreateView):
...
def get_initial(self):
initial_data = super(CreateDog, self).get_initial()
initial_data["owner"] = [(self.request.user.id, 
self.request.user.username)]
return initial_data




On Sunday, 28 January 2018 11:52:31 UTC+5:30, tangoward15 wrote:
>
> I updated my models. py
>
> from django.db import models
> from django.contrib.auth import get_user_model
> from django.contrib.auth.models import User, PermissionsMixin
>
> # Create your models here.
>
>
> Pet_Owner = get_user_model()
>
>
> class RegisteredUser(User, PermissionsMixin):
>
> def __str__(self):
> return self.username
>
>
> class Dog(models.Model):
> name = models.CharField(max_length=40)
> bday = models.DateField()
> owner = models.ForeignKey(Pet_Owner, related_name='dogs', 
> on_delete=models.CASCADE)
>
> def __str__(self):
> return self.name + ' - ' + str(self.owner)
>
>
> class Cat(models.Model):
> name = models.CharField(max_length=40)
> bday = models.DateField()
> owner = models.ForeignKey(Pet_Owner, related_name='cats', 
> on_delete=models.CASCADE)
>
> def __str__(self):
> return self.name + ' - ' + str(self.owner)
>
>
> I can see all users as drop down list for owner field using Pet_Owner = 
> get_user_model() .Problem is, it shows all the Users in the project not the 
> only one who is currently logged in.
>
> On Sun, Jan 28, 2018 at 2:06 PM, Akhil Lawrence  > wrote:
>
>> Its the reverse. If the owner is deleted, all the cats and dogs 
>> associated with the owner will be deleted.
>>
>> On Sunday, 28 January 2018 10:43:54 UTC+5:30, tangoward15 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  
>>> 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.
 To post to this group, send email to django...@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/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...@googlegroups.com .
>> To post to this group, send email to django...@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/09d7c9ca-7dab-4787-94d9-09b3a1bc682a%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/d5f91e94-bd91-47d4-9a26-bfbd56283f00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: CRUD code feedback

2018-01-27 Thread tango ward
I updated my models. py

from django.db import models
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User, PermissionsMixin

# Create your models here.


Pet_Owner = get_user_model()


class RegisteredUser(User, PermissionsMixin):

def __str__(self):
return self.username


class Dog(models.Model):
name = models.CharField(max_length=40)
bday = models.DateField()
owner = models.ForeignKey(Pet_Owner, related_name='dogs',
on_delete=models.CASCADE)

def __str__(self):
return self.name + ' - ' + str(self.owner)


class Cat(models.Model):
name = models.CharField(max_length=40)
bday = models.DateField()
owner = models.ForeignKey(Pet_Owner, related_name='cats',
on_delete=models.CASCADE)

def __str__(self):
return self.name + ' - ' + str(self.owner)


I can see all users as drop down list for owner field using Pet_Owner =
get_user_model() .Problem is, it shows all the Users in the project not the
only one who is currently logged in.

On Sun, Jan 28, 2018 at 2:06 PM, Akhil Lawrence 
wrote:

> Its the reverse. If the owner is deleted, all the cats and dogs associated
> with the owner will be deleted.
>
> On Sunday, 28 January 2018 10:43:54 UTC+5:30, tangoward15 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 
>> 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.
>>> To post to this group, send email to django...@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/ms
>>> gid/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/09d7c9ca-7dab-4787-94d9-09b3a1bc682a%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/CAA6wQLJHAucw119D%2BfjO_Y4ADANe82PSjFk4vxVJdNU_ESurWw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: CRUD code feedback

2018-01-27 Thread Akhil Lawrence
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 ")
> dog = Dog(name="mydog", bday=datetime.today(), owner=owner).save()
>
>
> On Sun, Jan 28, 2018 at 1:13 PM, tango ward  > 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 > > 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 .
>>> To post to this group, send email to django...@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/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.


Re: CRUD code feedback

2018-01-27 Thread Akhil Lawrence
Its the reverse. If the owner is deleted, all the cats and dogs associated 
with the owner will be deleted.

On Sunday, 28 January 2018 10:43:54 UTC+5:30, tangoward15 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  > 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 .
>> To post to this group, send email to django...@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/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/09d7c9ca-7dab-4787-94d9-09b3a1bc682a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: CRUD code feedback

2018-01-27 Thread tango ward
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="
b...@blah.com")
dog = Dog(name="mydog", bday=datetime.today(), owner=owner).save()


On Sun, Jan 28, 2018 at 1:13 PM, tango ward  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 
> 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+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/ms
>> gid/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/CAA6wQLLNJE0vO%3DGbMEy00C9RO-J3dnT-hBOxAPcVYc-Y1t6_fA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: CRUD code feedback

2018-01-27 Thread tango ward
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 
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+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/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/CAA6wQLKoJ9TkUo-CvhxVFKVkyeHfVS-%2Bk4HRQNPk_1OW5%2B_Oqw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: CRUD code feedback

2018-01-27 Thread Akhil Lawrence
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+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/340fa5fd-1475-4355-821c-82404b71b6bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: CRUD code feedback

2018-01-27 Thread tango ward
Hi,

Thanks for the response Akhil,

I am using the CreateView in my view.py to create ne pets


class CreateDog(CreateView):
model = Dog
fields = ('name', 'bday')
template_name = 'animals/dog_create.html'


class CreateCat(CreateView):
model = Cat
fields = ('name', 'bday')
template_name = 'animals/cat_create.html'



On Sun, Jan 28, 2018 at 11:49 AM, Akhil Lawrence 
wrote:

> Hi Jarvis,
>
> The code which you have posted only shows the model. Within your cat and
> dog models the owner can be null. Which explains why the owner is not
> associated with the pets. Can I see the code you tried to insert the
> records?
>
> ## changes needed in models
> models.ForeignKey(Owner, related_name='cats', on_delete=models.CASCADE)
> models.ForeignKey(Owner, related_name='dogs', on_delete=models.CASCADE)
>
> ## code for inserting records
> owner = Owner.objects.create_user(username="blah", password="blah",
> email="b...@blah.com")
> dog = Dog(name="mydog", bday=datetime.today(), owner=owner).save()
>
>
> Thanks.
>
>
> On Sunday, 28 January 2018 07:10:31 UTC+5:30, tangoward15 wrote:
>>
>>
>> Hi,
>>
>> I am playing around with CRUD. I want a user a create an account first
>> before he/she can add pets under his/her profile. I tried adding one pet
>> however it's seems that it is not associated to the Owner who added the pet.
>>
>> models.py
>>
>> from django.db import models
>> from django.contrib.auth.models import User, PermissionsMixin
>> from django.urls import reverse
>> # Create your models here.
>>
>>
>> class Owner(User, PermissionsMixin):
>>
>> def __str__(self):
>> return self.username
>>
>>
>> class Cat(models.Model):
>> name = models.CharField(max_length=40)
>> bday = models.DateField()
>> owner = models.ForeignKey(Owner, related_name='cats', null=True,
>> on_delete=models.SET_NULL)
>>
>> def get_absolute_url(self):
>> return reverse('test')
>>
>> def __str__(self):
>> return self.name
>>
>>
>> class Dog(models.Model):
>> name = models.CharField(max_length=40)
>> bday = models.DateField()
>> owner = models.ForeignKey(Owner, related_name='dogs', null=True,
>> on_delete=models.SET_NULL)
>>
>> def get_absolute_url(self):
>> return reverse('test')
>>
>> def __str__(self):
>> return self.name
>>
>>
>> Any suggestions will be highly appreciated.
>>
>>
>> Regards,
>> Jarvis
>>
> --
> 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/8f31afb0-2a9d-49ef-94b4-213aef140cf2%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/CAA6wQL%2BxQMxL7QVhWs%2Bx_mYST%3D5GRStGgEtjhz6XHvu49AFuEw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: CRUD code feedback

2018-01-27 Thread Akhil Lawrence
Hi Jarvis,

The code which you have posted only shows the model. Within your cat and 
dog models the owner can be null. Which explains why the owner is not 
associated with the pets. Can I see the code you tried to insert the 
records?

## changes needed in models
models.ForeignKey(Owner, related_name='cats', on_delete=models.CASCADE)
models.ForeignKey(Owner, related_name='dogs', on_delete=models.CASCADE)

## code for inserting records
owner = Owner.objects.create_user(username="blah", password="blah", 
email="b...@blah.com")
dog = Dog(name="mydog", bday=datetime.today(), owner=owner).save()


Thanks.

On Sunday, 28 January 2018 07:10:31 UTC+5:30, tangoward15 wrote:
>
>
> Hi,
>
> I am playing around with CRUD. I want a user a create an account first 
> before he/she can add pets under his/her profile. I tried adding one pet 
> however it's seems that it is not associated to the Owner who added the pet.
>
> models.py
>
> from django.db import models
> from django.contrib.auth.models import User, PermissionsMixin
> from django.urls import reverse
> # Create your models here.
>
>
> class Owner(User, PermissionsMixin):
>
> def __str__(self):
> return self.username
>
>
> class Cat(models.Model):
> name = models.CharField(max_length=40)
> bday = models.DateField()
> owner = models.ForeignKey(Owner, related_name='cats', null=True, 
> on_delete=models.SET_NULL)
>
> def get_absolute_url(self):
> return reverse('test')
>
> def __str__(self):
> return self.name
>
>
> class Dog(models.Model):
> name = models.CharField(max_length=40)
> bday = models.DateField()
> owner = models.ForeignKey(Owner, related_name='dogs', null=True, 
> on_delete=models.SET_NULL)
>
> def get_absolute_url(self):
> return reverse('test')
>
> def __str__(self):
> return self.name
>
>
> Any suggestions will be highly appreciated.
>
>
> Regards,
> Jarvis
>

-- 
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/8f31afb0-2a9d-49ef-94b4-213aef140cf2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.