Sorry, for asking again. In option one, It appears that I am getting an error for using the owner field which is a ForeignKey.
"
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()?"
"<SimpleLazyObject: <User: owner2>>": "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)
On Sun, Jan 28, 2018 at 5:32 PM, tango ward <tangoward15@gmail.com> 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 <akhilputhiry@gmail.com> 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 default2. 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.userreturn super(CreateDog, self).form_valid(form)For more details: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()return initial_dataFor more details: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 itThanks.
On Sunday, 28 January 2018 12:29:58 UTC+5:30, tangoward15 wrote:Sorry for being a drag, I tried this code:still I am getting the Admin user in the drop down list for owner.
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_dataOn Sun, Jan 28, 2018 at 2:31 PM, Akhil Lawrence <akhilp...@gmail.com> wrote:To view this discussion on the web visit https://groups.google.com/d/ms--override get_initial and set only the logged in userclass 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. pyI 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.
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)On Sun, Jan 28, 2018 at 2:06 PM, Akhil Lawrence <akhilp...@gmail.com> wrote:To view this discussion on the web visit https://groups.google.com/d/msIts 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 <akhilp...@gmail.com> 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%40goog legroups.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 .gid/django-users/09d7c9ca-7dab .-4787-94d9-09b3a1bc682a%40goog legroups.com
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 .gid/django-users/d5f91e94-bd91 .-47d4-9a26-bfbd56283f00%40goog legroups.com
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+unsubscribe@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/f8be9ac2-1738 .-4674-83b6-8baa2de8b446%40goog legroups.com
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+unsubscribe@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/CAA6wQLLsGdMy5jy7H4WLp%2BkJtqV52Zzum9kc0nNUR2Kdcvz%2Bnw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment