Django-admin set foreign key to Null for inlines selected item instead of delete

Skip to first unread message

Aeh. ABID

unread,
Jul 13, 2014, 2:48:05 PM7/13/14
to django...@googlegroups.com
   

I Have a model AB that holds two foreign keys A_id and B_id.

class AB(models.Model):
    A_id = models.ForeignKey('A')
    B_id = models.ForeignKey('B')
    field_1 = models.CharField(max_length=200, blank=True)
    field_2 = models.CharField(max_length=200, blank=True)


When editing A or B, AB items are edited inlines, what I want to achieve is that when editing let's say B I want to keep the selected AB items and set the foreign key B_id to null instead of deleting them.

thanks for any hint


http://stackoverflow.com/q/24726046/288387?sem=2

Mike Dewhirst

unread,
Jul 13, 2014, 6:27:09 PM7/13/14
to django...@googlegroups.com
On 14/07/2014 4:48 AM, Aeh. ABID wrote:
>
>
> I Have a model AB that holds two foreign keys A_id and B_id.
>
> |class AB(models.Model):
> A_id= models.ForeignKey('A')
> B_id= models.ForeignKey('B')
> field_1= models.CharField(max_length=200, blank=True)
> field_2= models.CharField(max_length=200, blank=True)|
>
>
> When editing A or B, AB items are edited inlines, what I want to achieve
> is that when editing let's say B I want to keep the selected AB items
> and set the foreign key B_id to null instead of deleting them.

Just do B_id = None in the save() method of AB ...

>
> thanks for any hint
>
>
> http://stackoverflow.com/q/24726046/288387?sem=2
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/f336fd6a-896c-4ed0-b55e-8e2a37d35d60%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/f336fd6a-896c-4ed0-b55e-8e2a37d35d60%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Aeh. ABID

unread,
Jul 13, 2014, 7:23:51 PM7/13/14
to django...@googlegroups.com
i'm bit confused here, shouldn't ovverride delete() instead of save() ? and how to retrieve selected items ?

Mike Dewhirst

unread,
Jul 13, 2014, 8:15:53 PM7/13/14
to django...@googlegroups.com
On 14/07/2014 9:23 AM, Aeh. ABID wrote:
> i'm bit confused here, shouldn't ovverride delete() instead of save() ?
> and how to retrieve selected items ?

Not really. You don't want to delete anything. You want to break a link.
Making the foreign key field null will do that.

It sounds like you might want a many to many relationship between A and
AB and another one between B and AB.

In that case, you actually delete the link record not the A nor the AB
record.

That makes AB.A_id and AB.B_id redundant. The only fields you need in AB
are field_1 and field_2.

So create a table A_AB and another one B_AB. A_AB needs a foreign key to
A and also a foreign key to AB. In addition, A needs a ManyToManyField
pointing to AB (or AB needs one to A) using the "through" table A_AB. Do
likewise between B and AB.

https://docs.djangoproject.com/en/1.6/ref/models/fields/#manytomanyfield

Mike

>
> On Sunday, July 13, 2014 7:48:05 PM UTC+1, Aeh. ABID wrote:
>
>
>
> I Have a model AB that holds two foreign keys A_id and B_id.
>
> |class AB(models.Model):
> A_id= models.ForeignKey('A')
> B_id= models.ForeignKey('B')
> field_1= models.CharField(max_length=200, blank=True)
> field_2= models.CharField(max_length=200, blank=True)|
>
>
> When editing A or B, AB items are edited inlines, what I want to
> achieve is that when editing let's say B I want to keep the selected
> AB items and set the foreign key B_id to null instead of deleting them.
>
> thanks for any hint
>
>
> http://stackoverflow.com/q/24726046/288387?sem=2
> <http://stackoverflow.com/q/24726046/288387?sem=2>
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/ec5cb8fa-a29d-4765-8e68-23a11cf6a877%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/ec5cb8fa-a29d-4765-8e68-23a11cf6a877%40googlegroups.com?utm_medium=email&utm_source=footer>.

Aeh. ABID

unread,
Jul 14, 2014, 8:11:42 AM7/14/14
to django...@googlegroups.com
Indeed introducing m2m relationship was just the solution for this case, now I have B form change with AB_B as  Inlines tabular item.
thanks a lot


On Sunday, July 13, 2014 7:48:05 PM UTC+1, Aeh. ABID wrote:

ROHITH KUMAR Miryala

unread,
Jul 15, 2014, 8:38:07 AM7/15/14
to django...@googlegroups.com



B-id=models.ForeignKey('B',on_delete=models.SET_NULL)

Aeh. ABID

unread,
Jul 16, 2014, 6:13:23 AM7/16/14
to django...@googlegroups.com
This is not for my use case, it was about setting to null foreign key when selecting inline items to be deleted

On Tuesday, July 15, 2014 1:38:07 PM UTC+1, ROHITH KUMAR Miryala wrote:



B-id=models.ForeignKey('B',on_delete=models.SET_NULL)
Reply all
Reply to author
Forward
0 new messages