I am having a confusion on how to model a relationship in Django so that it can be edited inline in the Django Admin.
Let me explain the scenario.
I have a Customer Model and An Address Model. In the customer model I have a OneToOneField relationship to Address once for billing and once for shipping address.
class Address(models.Model):
pass
class Employee(models.Model):
billing_address = models.OneToOneField(Address)
shipping_address = models.OneToOneField(Address)
# Many more such fields
Now with this model there is no easy way to make them inline in Admin. I have tried the following
class AddressInline(admin.TabularInline):
model = Address
class Customer(admin.ModelAdmin):
inlines = [AddressInline, ]
I keep getting an error,
<class 'employee.admin.AddressInline'>: (admin.E202) 'employee.Address' has no ForeignKey to 'employee.Customer'.
Now I know there are other bugs similar to this one. ie. Use OneToOneField inlined in Django Admin and Django admin - OneToOneField inline throws "has no ForeignKey" exception
But I think my question is slightly different to warrant this post. Please help!
#1
models.py
class BillingAddress(models.Model):
employee = models.OneToOneField(to=Employee, related_name='billing_address')
class Employee(models.Model):
# Many more such fields
admin.py
class AddressInline(admin.TabularInline):
model = BillingAddress
extra = 1
#admin.register(Employee)
class EmployeeAdmin(admin.ModelAdmin):
inlines = [AddressInline, ]
#2
models.py
class Address(models.Model):
name = models.CharField(max_length=250)
employee = models.ForeignKey(to=Employee, related_name='addresses')
class Employee(models.Model):
# Many more such fields
admin.py
class AddressInline(admin.TabularInline):
model = Address
#admin.register(Employee)
class EmployeeAdmin(admin.ModelAdmin):
inlines = [AddressInline, ]
Full Example:
models.py
class Company(models.Model):
pass
class CompanyScheduler(models.Model):
company = models.OneToOneField(
to=Company,
related_name='scheduling',
on_delete=models.CASCADE,
)
start = models.DateField()
finish = models.DateField()
admin.py
class CompanySchedulerInLine(admin.TabularInline):
model = CompanySchedulerInLine
extra = 1
#admin.register(CompanyModelAdmin)
class CompanyModelAdmin(admin.ModelAdmin):
inlines = [
SchedulerInLine,
]
UI
Related
class Item(models.Model):
name=models.CharField(max_length=250)
description = model.TextField()
class Photo(models.Model):
item = models.ChaField(max_length=250)
title=models.ChaField(max_length=250)
here is admin.py
class PhotoInline(admin.StackedInline):
model = Photo
class ItemAdmin(admin.ModelAdmin):
inlines = [PhotoInline]
admin.site.register(Item,strong text ItemAdmin)
admin.site.register(Photo)
i want to have an inline connection between without them having any relationship
How to have multiple tables(queryset) on one page in django admin.
For example:
When i go to the company page, i can see the list of departments in the company, i can also see the list of employees in the company.
You can use InlineModelAdmin objects to implement this, although if you want to do nested inlines you should check out this post. Although as that post says:
...it would be a kind of convoluted design to implement.
You didn't provide any code here so the best I can do is guess your model relationships.
models.py
from django.db import models
class Department(models.Model):
name = models.CharField(max_length=250)
...
class Employee(models.Model):
name = models.CharField(max_length=250)
...
class Company(models.Model):
name = models.CharField(max_length=250)
departments = models.ForeignKey(Department)
employees = models.ForeignKey(Employee)
...
admin.py
from django.contrib import admin
class EmployeeInline(admin.StackedInline):
model = Employee
class DepartmentInline(admin.StackedInline):
model = Department
class CompanyAdmin(admin.ModelAdmin):
list_display = ('name')
inlines = [DepartmentInline, EmployeeInline]
admin.site.register(CompanyAdmin)
Assuming I have :
class Product(models.Model):
[...]
class Basket(models.Model):
content = models.ManyToManyField(Product, through="ProductQuantity")
class ProductQuantity(models.Model):
basket = models.ForeignKey(Basket)
product = models.ForeignKey(Product)
quantity = models.IntegerField(default=0)
How could I render a ModelForm for the Basket Model with a field for each ProductQuantity of a Basket, just to be able to modify its quantity attribute?
Is there a widget I could use for this?
If I was able to do such thing with such a ModelForm, could I use this ModelForm in an admin.ModelAdmin as an alternative form attribute to have the same behaviour in the admin interface?
Edit :
#MuhammadTahir marked this post as possible duplicate of this post.
It indeed helped me to understand better, but I'm still stuck : I can't render the fields I want to render.
Here is my code so far :
models.py
Same as above.
forms.py
ProductQuantityFormSet = inlineformset_factory(Product,
basket.content.through,
fields=("quantity",))
admin.py
class ProductQuantityInline(admin.StackedInline):
model = ProductQuantity
formset = ProductQuantityFormSet()
class BasketAdmin(admin.ModelAdmin):
inline = [ProductQuantityInline,]
Someone on IRC found the problem :
inline ≠ inlines in the admin.ModelAdmin. And I don't even need the inlineformset_factory().
So here is the final code :
models.py
class Product(models.Model):
[...]
class Basket(models.Model):
content = models.ManyToManyField(Product, through="ProductQuantity")
class ProductQuantity(models.Model):
basket = models.ForeignKey(Basket)
product = models.ForeignKey(Product)
quantity = models.IntegerField(default=0)
admin.py
class ProductQuantityInline(admin.StackedInline):
model = ProductQuantity
fields = ["quantity",]
class BasketAdmin(admin.ModelAdmin):
inlines = [ProductQuantityInline,]
I hope this could help somebody else. :)
I have a django model named Event with a generic inline relation to Relationship, like this:
# models.py
class Person(models.Model):
...
class Role(models.Model):
...
class Event(models.Model):
...
class Relationship(models.Model):
person = models.ForeignKey(Person)
role = models.ForeignKey(Role)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey("content_type", "object_id")
# admin.py
class RelationshipInline(generic.GenericTabularInline):
model = Relationship
extra = 0
class EventAdmin(admin.ModelAdmin):
inlines = [RelationshipInline]
I'd like to find a way to edit the inlines not only from the event admin page, but from the people admin page too. SO far I have added the following code to display inlines in the people page too
class ReverseRelationshipInline(admin.TabularInline):
model = Relationship
class IndividualAdmin(admin.ModelAdmin):
inlines = [ReverseRelationshipInline]
But I get content_type and object_id fields in the form and it is not very informational for admin users since it is just references to primary keys. I would rather prefer to resolve and show the content_object(even if it is not editable, to know at list to what objects are people related to).
Any direction to recommend?
Thanks.
Your "ReverseRelationshipInline" has to be a GenericTabularInline, not a TabularInline. That's all :-)
UPDATE
I think I now understand what you're after, and my answer is:
You won't be able to edit the content object inline of Person, but you want to show it nicely, maybe even as a link to its change form.
Add a function to Relationship which returns such an HTML link, provide your own ModelForm to your inline and specify the fields you want, which now includes your new function value (read-only). Something like this (untested):
# models.py
from django.core import urlresolvers
class Relationship(models.Model):
...
def link_content_object_changeform(self):
obj = self.content_object
change_url = urlresolvers.reverse(
'admin:%s_%s_change' % (
obj._meta.app_label,
obj._meta.object_name.lower()
),
args=(obj.id,)
)
return u'%s' % (change_url, obj.__unicode__())
link_content_object_changeform.allow_tags = True
link_content_object_changeform.short_description = 'in relation to'
# admin.py
class ReverseRelationshipInlineForm(forms.ModelForm):
class Meta:
model = Relationship
fields = ('person', 'role', 'link_content_object_changeform')
readonly_fields = ('link_content_object_changeform',)
class ReverseRelationshipInline(admin.TabularInline):
model = Relationship
form = ReverseRelationshipInlineForm
I recently upgraded to Django 1.2.1 because I was specifically interested in the ability to have basic many-to-many inline fields. When using the admin like so:
Initial models:
class Ingredient(models.Model):
name = models.TextField()
class Recipe(models.Model):
ingredients = models.ManyToManyField(Ingredient)
Initial admin:
class IngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeOptions(admin.ModelAdmin):
inlines = [IngredientInline,]
exclude = ('ingredients',)
admin.site.register(Recipe,RecipeOptions)
What I got was the same form you would normally see on a ManyToMany field, with some extra rows. Supplying it with extra parameters like an Ingredient ModelForm did not help. Suspecting that something might be wrong with the basic ModelForm associations via model = Foo.manyfields.through, I decided to see if an intermediary model would help. It now displays a working inline form via:
New models:
class RecipeJoin(models.Model):
pass
class Recipe(models.Model):
ingredients = models.ManyToManyField(RecipeJoin,through='Ingredient')
class Ingredient(models.Model):
name = models.TextField()
test = models.ForeignKey(RecipeJoin,null=True,blank=True,editable=False)
New admin:
class IngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeOptions(admin.ModelAdmin):
inlines = [IngredientInline,]
admin.site.register(Recipe,RecipeOptions)
Obviously this is not a hack I'd like to use. Anyone know of a way to get a manytomany relationship to display via inline form without either (a) creating an entirely new BasicInline form and template or (b) putting it through an intermediary (or generic admin) model?
TIA. (I apologize for verbosity, it's my first post so wanted to be thorough).
Do one of these examples accomplish what you are trying to do?
a:
# Models:
class Ingredient(models.Model):
name = models.CharField(max_length=128)
class Recipe(models.Model):
name = models.CharField(max_length=128)
ingredients = models.ManyToManyField(Ingredient, through='RecipeIngredient')
class RecipeIngredient(models.Model):
recipe = models.ForeignKey(Recipe)
ingredient = models.ForeignKey(Ingredient)
amount = models.CharField(max_length=128)
# Admin:
class RecipeIngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeAdmin(admin.ModelAdmin):
inlines = [RecipeIngredientInline,]
class IngredientAdmin(admin.ModelAdmin):
pass
admin.site.register(Recipe,RecipeAdmin)
admin.site.register(Ingredient, IngredientAdmin)
b:
# Models:
class Recipe(models.Model):
name = models.CharField(max_length=128)
class Ingredient(models.Model):
name = models.CharField(max_length=128)
recipe = models.ForeignKey(Recipe)
# Admin:
class IngredientInline(admin.TabularInline):
model = Ingredient
class RecipeAdmin(admin.ModelAdmin):
inlines = [IngredientInline,]
admin.site.register(Recipe,RecipeAdmin)
If I remember correctly (and it's been a while since I've done this part), you need to add the admin for Ingredient and set it to have the custom ModelForm. Then that form will be used in the inline version of Ingredient.