Rendering TabuarInline inside ModelAdmin without a foreign key

45 views
Skip to first unread message

Mario Gudelj

unread,
Oct 28, 2014, 11:57:36 PM10/28/14
to django...@googlegroups.com
Hi list,

I have a table of orders where one of the columns is a IntegerField containing the id of a user who created the order. 

Since it's not a FK field django admin can't display these orders inline inside the user details page.

I would normally use something like:

class OrderInline(admin.TabularInline):
    extra = 0
    model = Order

And then add that to the user model like this:

inlines = [OrderInline]

But it won't work without the FK.

I've Googled around and tried everything I found on SO but I can't figure out how to add orders inline to my user model.

I'm looking at https://docs.djangoproject.com/en/dev/_modules/django/contrib/contenttypes/admin/#GenericTabularInline and I'm thinking do I need to write a similar class or is there something easy and simple I'm missing.

Cheers,

Mario


Daniel Roseman

unread,
Oct 29, 2014, 6:26:52 AM10/29/14
to django...@googlegroups.com
You don't show your models, but why is it not a ForeignKey? After all, a FK field is exactly what you describe, an integer field that contains the ID of the model it is pointing to. So why not declare it as one?
--
DR. 

Mario Gudelj

unread,
Oct 29, 2014, 4:07:00 PM10/29/14
to django...@googlegroups.com

I did not produce the original model so I wouldn't know. My thinking is that the order model had to be independent of the user model so that the deletion of the user doesn't cascade down to orders. It's a mezzanine site and that's how it handles fk from orders to users.

--
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/fc21c60e-5a33-4e10-9f48-42c2c8c43c7e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mario Gudelj

unread,
Oct 29, 2014, 7:13:53 PM10/29/14
to django...@googlegroups.com
Here's the models code, DR:

class CRMUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(
        verbose_name='Email Address',
        max_length=255,
        unique=True,
    )
    first_name = models.CharField("First Name", max_length=50, blank=True)
    last_name = models.CharField("Last Name", max_length=50, blank=True)
    phone = models.CharField("Billing Country", max_length=30, null=True, blank=True)
    ...

class Order(models.Model):
    order_code = models.CharField("Order ID", null=True, max_length=10)
    status = models.IntegerField("Order Status", choices=ORDER_STATUS_CHOICES, null=True, default=1)
    user_id = models.IntegerField("User ID", null=True, blank=True)
    ...

Thanks for all your help on SO and this list by the way. I seem to come across your answers a lot and they're really useful. You're a champ!

Collin Anderson

unread,
Nov 2, 2014, 7:55:04 PM11/2/14
to django...@googlegroups.com
Hi Mario,

If you are able to edit the model replacing the IntegerField with this should do what you want:
user = models.ForeignKey(CRMUser, null=True, blank=True, on_delete=DO_NOTHING)

Collin

Mario Gudelj

unread,
Nov 2, 2014, 10:03:42 PM11/2/14
to django...@googlegroups.com

Thanks Collin. That's the path I've had to take.

--
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.
Reply all
Reply to author
Forward
0 new messages