Wednesday, October 29, 2014

Re: Rendering TabuarInline inside ModelAdmin without a foreign key

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!

On 30 October 2014 07:06, Mario Gudelj <mario.gudelj@gmail.com> wrote:

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.

On 29/10/2014 9:27 pm, "Daniel Roseman" <daniel@roseman.org.uk> wrote:
On Wednesday, 29 October 2014 03:57:36 UTC, somecallitblues wrote:
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



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. 

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

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHqTbjmRmLJ1OYP6ptLaJ6U9Wvq3oT1YWiu-URMAE19ahcfsGg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment