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

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

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

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

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.

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):

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

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

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',

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

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

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"] =

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

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):

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

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

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()

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

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):

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')

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,

CRUD code feedback

2018-01-27 Thread tango ward
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