I have these two models:
class Rule(models.Model):
name = models.CharField(max_length=200)
class Channel(models.Model):
id = models.CharField(max_length=9, primary_key=True)
name = models.CharField(max_length=100)
rule = models.ForeignKey(Rule, related_name='channels', blank=True)
And I have to be able to add channels to rule in admin site within RuleAdmin interface. So I created these two admin models:
class ChannelAdmin(admin.TabularInline):
model = Channel
class RuleAdmin(admin.ModelAdmin):
model = Rule
inlines = [ChannelAdmin]
But when I start my server I got this errors:
ERRORS:
<class 'main.admin.ChannelAdmin'>: (admin.E202) 'main.Channel' has no ForeignKey to 'main.Channel'.
Still in django shell I can make queries like
rule = Rule.objects.get(pk=1)
rule.channels.all()
There is got to be something obvious but I just can't figure it out.
One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.
To automate this process, we can programmatically fetch all the models in the project and register them with the admin interface. Open admin.py file and add this code to it. This will fetch all the models in all apps and registers them with the admin interface.
do something like this:
class ChannelAdmin(admin.TabularInline):
model = Channel
class RuleAdmin(admin.ModelAdmin):
inlines = [ChannelAdmin,]
admin.site.register(Rule,RuleAdmin)
class OrderItemInline(admin.TabularInline):
model = OrderItem
fields = ['image']
class OrderAdmin(admin.ModelAdmin):
list_display = ['id']
list_filter = ['status']
inlines = [OrderItemInline]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With