delete OnetoOneField link

59 views
Skip to first unread message

madjardi

unread,
Dec 30, 2014, 4:56:41 AM12/30/14
to django...@googlegroups.com
class Cashbox(models.Model):
    cashier
= models.OneToOneField('User', null=True, blank=True, related_name='cashbox', on_delete=models.SET_NULL)

in other code:
User.object.get(pk=1)
user
.cashbox = None

AttributeError: 'NoneType' object has no attribute 'cashier_id'

one version solving is:
c1 = Cashbox.objects.get(pk=user.cashbox.id)
c1
.cashier = None
c1
.save()


c2
= Cashbox.object.get(pk=10)
c2
.cashier = user
c2
.save()


i need get other Cashbox to set link None. Why i need do so. is there a better solution.

Vijay Khemlani

unread,
Dec 30, 2014, 9:19:53 AM12/30/14
to django...@googlegroups.com
If you want to change a user's cashbox then yes, you would need to use your solution, but it's easier to get c1 by just saying

c1 = user.cashbox

instead of Cashbox.objects.get....

A more definite solution would be to add the OneToOne relation to the User model instead of the Cashbox object, but I'm not sure how that works in your project.

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9bac63fa-64e3-4353-82ae-6b31fa8f281f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

madjardi

unread,
Dec 30, 2014, 3:03:18 PM12/30/14
to django...@googlegroups.com
thank you. you give me more confidence

вторник, 30 декабря 2014 г., 17:19:53 UTC+3 пользователь Vijay Khemlani написал:
Reply all
Reply to author
Forward
0 new messages