Multi-table inheritance in the admin view - django

I have the following two models (multi-table inheritance):
class Funnies(models.Model):
title = models.CharField(max_length=200)
class FunniesProfile(Funnies):
body = models.TextField()
I register both using the admin.site.register(). When I want to add a new "Funnies" entry I get the "title" field only. However, I would also like to get the field for the "body" so when an admin creates a new Funnies entry he will have the option to add a FunniesProfile entry that will automatically get the ptr_id of the Funnies entry. How can I do that?
Meir

You can probably use this instead:
models.py:
class Funnies(models.Model):
title = models.CharField(max_length=200)
class FunniesProfile(models.Model):
funnies = models.OneToOneField(Funnies)
body = models.TextField()
admin.py:
class FunniesProfileInline(admin.TabularInline):
model = FunniesProfile
class FunniesAdmin(admin.ModelAdmin):
inlines = [
FunniesProfileInline,
]
site.register(Funnies, FunniesAdmin)

Related

How can I include a ManyToMany field into django admin fieldsets?

I have two models:
Python 3.7
class ClassOne(models.Model):
name = models.CharField(max_length=10, default='')
class ClassTwo(models.Model):
name = models.CharField(max_length=20, default='')
class_ones = models.ManyToManyField(ClassOne)
Now I would like to show ClassOne in django-admin with all ClassTwo's listed. I already tried to create a Tabular (admin.TabularInline) or create a method in ClassOne as following:
def get_class_twos(self):
return self.classtwo_set.all()
and include that method in the fieldsets, but that did not work either. Neither did directly putting classtwo_set.all() or classtwo_set in the fieldset list.
You first should define TabularInline:
class ClassOneInLine(admin.TabularInline):
model = ClassTwo.classone.through
form = ClassOneForm
Then add it to admin class for ClassTwo:
class AuthorAdmin(admin.ModelAdmin):
inlines = (ClassOneInLine,)
And don't forget to register admin.

Django admin page combobox, possible?

I have a simple model in django with a field using choices. As it is now, I am forced to use one of the available choices, how can I be able to use those choices and also be able to enter text manually?
WHERE_CHOICES = (
('CHL', 'Center HL'),
('CHD', 'Center HD')
)
class Event(models.Model):
ev_start = models.DateTimeField()
ev_end = models.DateTimeField()
where = models.CharField(max_length=3, default='CHL', choices=WHERE_CHOICES)
Thanks
You can override the admin form field widget with a datalist tag.
It would look like that (using the floppyforms library):
class EventAdminForm(ModelForm):
class Meta:
model = Event
fields = "__all__"
widgets = {
'where': floppyforms.widgets.Input(datalist=[v[0] for v in Event.WHERE_CHOICES])
}
class EventAdmin(admin.ModelAdmin):
form = EventAdminForm
admin.site.register(Event, EventAdmin)

Edit manytomany field right from Admin form for model

I have models like below
class Product(models.Model):
...
class ProductQuantity(models.Model):
product = models.ForeignKey('Product')
invoice = models.ForeignKey('Invoice')
quantity = models.IntegerField()
class Invoice(models.Model):
...
products = models.ManyToManyField(Product, through=ProductQuantity)
In django admin I would like to change quantity not by opening new dialog, which is basically how admin behaves, instead selecting one of them if any, or type there and change values directly from that window.
You can use an inline:
class ProductQuantityInline(admin.StackedInline):
model = ProductQuantity
class InvoiceAdmin(admin.ModelAdmin):
inlines = [ProductQuantityInline]
That way, you can edit the ProductQuantity directly on the Invoice admin page, without any extra dialogs.

Howto define a formset in django (like multiple inline)

For couple days I am trying to do multiple inline models in django. (inline inside inline)
But I couldnt. Then I decided to define a formset while editing base model (Page in example below), So one can also add new link when adding a new page(in admin page).
Like page->linksection->link. Here is the model.
#model.py
class Page(models.Model):
title = models.CharField(max_length=255)
class LinkSection(models.Model):
page = models.ForeignKey(Page)
title = models.CharField(max_length=255)
class Link(models.Model):
linksection = models.ForeignKey(LinkSection)
text = models.CharField(max_length=255)
url = models.URLField()
#admin.py
class LinkSectionInline(admin.TabularInline):
form = LinkSectionForm + LinkForm
Can you please show me how to define a formset for this model?
Thank you very much.

Django - Foreign key reverse creation

Imagine this model:
class ExercisePart(models.Model):
exercise = models.ForeignKey(Exercise)
sequence = models.IntegerField()
class Meta:
unique_together = (('exercise', 'sequence',),)
class Exercise(models.Model):
name = models.CharField(max_length=15)
From the admin interface I'd like to be able to create/link ExerciseParts through the Exercise page. I'd like to do that because I wish to avoid having to go on another page each time I want to add an ExerciseParts.
Is it possible ? How can I do that ?
You're looking for the inline admin feature.
admin.py
class ExercisePartInline(admin.TabularInline):
model = ExercisePart
class ExerciseAdmin(admin.ModelAdmin):
inlines = [ExercisePartInline]