I am encountering what seems to me a weird bug when rendering Inline forms on the "Add" view of a ModelAdmin.
Here is a minimum example with Django version 2.2.4.
in models.py:
class MyModel(models.Model):
text = models.CharField(max_length=100)
class RelatedModel(models.Model):
parent = models.ForeignKey(MyModel, null=False, on_delete=models.CASCADE)
number = models.DecimalField(decimal_places=2, max_digits=10, null=False, blank=False)
in admin.py:
class RelatedModelInlineTabular(admin.TabularInline):
model = RelatedModel
show_change_link = False
fields = ("number", )
class TestMyModelCreate(admin.ModelAdmin):
fields = ['text', ]
inlines = [RelatedModelInlineTabular]
admin.site.register(MyModel, TestMyModelCreate)
Steps to replicate
Login to django admin website
open the "add" view for MyModel (i.e. navigate to the list of Models and click on the "Add new" button)
Expected result
The form displays an empty text field. Below that, an Inline form is displayed with 3 empty rows for potential related instances of RelatedModel
Actual result
The Inline form is displayed twice, each instance with its own 3 empty rows, as if I had specified it twice.
I attach a screenshot below of the actual page (Discount is the name of the related Model). I tried and I get the same result with both StackedInline and TabularInline.
Am I making some trivial error here that could explain what's happening? Or is this is a known bug? Thank you in advance to anyone that will help.
Related
In a form I have a dropdown list , the default behaviour of django seems to fill the first entry with ----------.
How can I remove this, so it will just use the firs entry as the default?
(note that the entries may change, so hardcoding a value for initial= might not work. )
model:
class Job(models.Model):
...
category = models.ForeignKey(Category, on_delete=models.PROTECT)
Try with:
Test 1
class Job(models.Model):
...
category = models.ForeignKey(Category,default="Design" on_delete=models.PROTECT)
As a ForeignKey all related field will have Design as default
Test 2
override the display in Django Admin by create a form.py and register it in the admin.py file and set the default selected value
I have little experience in django so I would really appreciate your help!
In general, I have created a ModelForm which depending on the users who is logged in, he changes some values in the field. So consider that this form is being edited about 5 times by 5 different users.
I would like to show a specific field in the template (and view) only when the third user is logged in.
My model is :
class Task(models.Model):
Taskdetails = models.CharField(max_length=500, null=True)
asset = models.ForeignKey('Asset', null=True)
failure = models.ForeignKey('Failure', null=True)
cause = models.ForeignKey('Cause', null=True)
Created_task_date = models.DateTimeField(default=timezone.now, null=True)
employee = models.ForeignKey("auth.User", null = True)
and also i have created this ModelForm
class Meta:
model = Task
fields = ('Taskdetails', 'asset', 'failure', ,'employee','cause',)
Also I have 5 edititions of the TaskForm in which is user edits something.
The thing I am trying to do is to show the cause field only in the third form.
I tried to exclude the value but nothing apperas.
If i include the field cause (just like above), I must "pass" it in the template in order to be edited from the first user (otherwise the task_form is not saved)
I hope I became clear.
I would really appreciate your help.
I have a model Teacher which has following data:
class Teacher(models.Model):
name = models.CharField(max_length=100)
thumbnail = models.ForeignKey(Thumbnail)
class Thumbnail(models.Model):
thumbnail = models.FileField(upload_to=upload_to)
class TeacherAdmin(admin.ModelAdmin):
list_display = ('name',)
Here I want to relate all the thumbnail in my project to Thumbnail field. In my admin it shows me a dropdown of thumbnail while I want to add the thumbnail from admin.
Is it possible that I can upload from admin too?
Also what can I do in the case like :
class Teacher(models.Model):
name = models.CharField(max_length=100)
thumbnail = models.ManyToManyField(Thumbnail)
If I want to add multiple Images.
The only issue I am having is on admin side. Client side can be handles from regular forms.
Need suggestions and Ideas
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.
I'm trying to add a ManyToManyField to my Django app and it almost works. My only problem is that when I've saved an object and view it again in the admin the ManyToMany-fields doesn't get selected.
I set blank=True since otherwise it wouldn't let me save without selecting at least one item and I want the many-to-many-fields to be optional.
The whole many-to-many admin field is greyed out, that might have something to do with it.
http://dl.dropbox.com/u/3184097/manytomany.png
Model:
class Disease(models.Model):
name = models.CharField(max_length=100)
text = models.CharField(max_length=2000)
vaccines = models.ManyToManyField(Vaccine, blank=True)
countries = models.ManyToManyField(Country, blank=True)
def __unicode__(self):
return self.name
Admin:
from dbaccess.models import *
from django.contrib import admin
admin.site.register(Vaccine)
admin.site.register(Disease)
admin.site.register(Country)
admin.site.register(Medicine)
EDIT:
I checked and the disease_vaccine and disease_countries does contain items, so they are saved just not shown when the Disease is opened again in the Admin.
Try doing:
class DiseaseAdmin(admin.ModelAdmin):
pass
admin.site.register(Disease, DiseaseAdmin)