I have 2 models as follows. Now I need to inline Model A on Model B's page.
models.py
class A(models.Model):
name = models.CharField(max_length=50)
class B(models.Model):
name = models.CharField(max_length=50)
a = models.ForeignKey(A)
admin.py
class A_Inline(admin.TabularInline):
model = A
class B_Admin(admin.ModelAdmin):
inlines = [A_Inline]
is that possible?? If yes please let me know..
No, as A needs to have a ForeignKey to B to be used as an Inline. Otherwise how would the relationship be recorded once you save the inline A?
You cannot do it as told by timmy O'Mahony. But you can make B inline in A if you want. Or maybe you can manipulate how django display it in
def unicode(self):
models.py
class A(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class B(models.Model):
name = models.CharField(max_length=50)
a = models.ForeignKey(A)
admin.py
class B_Inline(admin.TabularInline):
model = B
class A_Admin(admin.ModelAdmin):
inlines = [
B_Inline,
]
admin.site.register(A, A_Admin)
admin.site.register(B)
Or maybe you want to use many-to-many relationship?
models.py
class C(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class D(models.Model):
name = models.CharField(max_length=50)
cs = models.ManyToManyField(C)
admin.py
class C_Inline(admin.TabularInline):
model = D.cs.through
class D_Admin(admin.ModelAdmin):
exclude = ("cs",)
inlines = [
C_Inline,
]
admin.site.register(C)
admin.site.register(D, D_Admin)
Of course you can do this. Every relationship be it 1-1, m2m or FK will have a reverse accessor. (more on the FK reverse access here)
class A_Inline(admin.TabularInline):
model = A.b_set.through
class B_Admin(admin.ModelAdmin):
inlines = [A_Inline]
Related
As I am using string representation of a model, it is also shown in a autocomplete_fields (Select2). But the problem is that when i try to search in the field, it is searching the model's name field, not string representation.
Here is my code example:
models.py
class Store(models.Model):
name = models.CharField(max_length=256)
class Department(models.Model):
name = models.CharField(max_length=256)
store = models.ForeignKey(Store, on_delete=models.CASCADE)
class Shelf(models.Model):
name = models.CharField(max_length=256)
department = models.ForeignKey(Department, on_delete=models.CASCADE)
def __string__(self):
return f'{self.department.store.name} {self.department.name} {self.name}'
class Product(models.Model):
name = models.CharField(max_length=256)
shelf = models.ForeignKey(Shelf, on_delete=models.CASCADE)
admin.py
#admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
autocomplete_fields = ('shelf',)
list_display = ('name', 'shelf')
Is it posible to search by model string representation in this case?
First of all your string method should be __str__ instead and since the str method is not a field you cannot add it to the autocomplete field since it looks for a field and works on the DB level.
What you can do is:
#admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
autocomplete_fields = ('shelf__department__store__name','shelf__department__name','name')
list_display = ('name', 'shelf')
I have two models where employee have relation with person model but person have no relation with employee model.
Like:
class Person(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
class Employee(models.Model):
person = models.ForeignKey(Person, related_name='person_info')
code = models.CharField()
In such cases I want code field data in person serializer.
I can solved this with writing method in person model or using SerializerMethodField in person serializer
like this:
def get_employee_code(self):
return Employee.objects.get(person=self).id
and add this as source in person serializer
employee_code = serializers.CharField(source='get_employee_code')
Or adding employee serializer into person serialiszer
class PersonSerializer(serializers.ModelSerializer):
employee = EmployeeSerializer()
class Meta:
model = Person
fields = ('name', 'address', 'employee')
But i was trying to do this with reverse relation but i can't. I have tried like this, it gives an error
Serializer:
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.CharField(source='person_info.code')
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
How can i solve this with reverse relation?
At the moment because you are using a ForeignKey field on the person attribute, it means that its returning a list when you access the reverse relation.
One solution would be to use a slug related field, though this must have many and read_only set to True, and will return a list because of the ForeignKey field.
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.SlugRelatedField(
source='person_info',
slug_field='code',
many=True,
read_only=True,
)
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
The other option is to change your ForeignKey into a OneToOneField, which would still need read_only set to True but it will not return a list.
class Person(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
class Employee(models.Model):
person = models.OneToOneField(Person, related_name='person_info')
code = models.CharField()
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.SlugRelatedField(
source='person_info',
slug_field='code',
read_only=True,
)
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
Or, if you don't want to change the ForeignKey, you could add a employee_code property method to the model instead to return the first employee code in the person_info relation.
class Person(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
#property
def employee_code(self):
employees = self.person_info.filter()
if employees.exists():
return employees.first().code
return ''
class Employee(models.Model):
person = models.OneToOneField(Person, related_name='person_info')
code = models.CharField()
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.CharField(
read_only=True,
)
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
you can access the reverse relation with custom SerializerMethodField()
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.SerializerMethodField()
def get_employee_code(self, obj):
return obj.person_info.code
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
Are there exists any examples of Django widgets which can be useful for ManyToManyFields with 'through' attributes? For example, i have these models (got the source from django documentation):
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=128)
def __str__(self): # __unicode__ on Python 2
return self.name
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
def __str__(self): # __unicode__ on Python 2
return self.name
class Membership(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
group = models.ForeignKey(Group, on_delete=models.CASCADE)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
Obvisously, standart ModelMultipleChoiceField won't work here. I need to populate 'date_joined' , and 'invite_reason' while adding. Which is the simpliest way to achieve this?
This is a bit too complex for a simple widget. I can't even imagine how it would look like. You will have to use inline formsets for that purpose.
This should give something like this:
from django.forms import inlineformset_factory
MembershipFormSet = inlineformset_factory(Group, Membership, fields=(
'person', 'date_joined', 'invite_reason'))
group = Group.objects.get(pk=group_id)
formset = MembershipFormSet(instance=group)
Within django.contrib.admin, you can use inlines with InlineModelAdmin.
I've the following models:
class A(models.Model):
title = models.CharField(max_length=30)
class B(models.Model):
title = models.CharField(max_length=255)
a = models.ManyToManyField(A)
I now want a model C which has a field that has multiple unique combinations of A and B like in ManyToMany field. I found that for mapping ForeignKey to two models GenericForeignKey can be used. I've tried GenericForeignKey like below:
class C(models.Model):
title = models.CharField()
property1 = models.CharField()
a = models.ManyToManyField(A)
b = models.ManyToManyField(B)
a_b = GenericForeignKey(store, category)
But, this doesn't seem to work. Can someone point me in the right direction? I'm new to Django as well as SQL.
Edit: Clarified the requirement of combinations.
Edit2: Current working model:
I've created below models as a working solution:
class A(models.Model):
title = models.CharField(max_length=30)
class B(models.Model):
title = models.CharField(max_length=255)
a = models.ManyToManyField(A)
class C(models.Model):
title = models.CharField()
property1 = models.CharField()
class A_B_C(models.Model):
a = models.ForeignKey(A)
b = models.ForeignKey(B)
c = models.ForeignKey(C)
class Meta:
unique_together = ('a', 'b', 'c')
It is this model that I was avoiding to create explicitly and maintain within relations. I would like to know if this solution would be efficient or is there another Django way to do things?
When using ManyToManyField Django creates intermediate table for you implicitly. In your example you'll get additional table with columns (id, a_id, b_id). But you can define it explicitly and set a through option on a field a on B model, and then define unique_together for this intermediate model:
class A(models.Model):
title = models.CharField(max_length=30)
class B(models.Model):
title = models.CharField(max_length=255)
a = models.ManyToManyField(A, through='C')
class C(models.Model):
a = models.ForeignKey(A, on_delete=models.CASCADE)
b = models.ForeignKey(B, on_delete=models.CASCADE)
class Meta:
unique_together = ("a", "b")
Note: If I remember correctly, you can't set unique_together on ManyToManyFiled directly.
This isn't a job for GenericForeignKey. Instead you need to define the unique_together Meta attribute:
class C(models.Model):
...
class Meta
unique_together = ('a', 'b')
For future reference:
I've created below models as a working solution:
class A(models.Model):
title = models.CharField(max_length=30)
class B(models.Model):
title = models.CharField(max_length=255)
a = models.ManyToManyField(A)
class C(models.Model):
title = models.CharField()
property1 = models.CharField()
class A_B_C(models.Model):
a = models.ForeignKey(A)
b = models.ForeignKey(B)
c = models.ForeignKey(C)
class Meta:
unique_together = ('a', 'b', 'c')
I'm trying to import data to one of my models, but it's failing because I'm trying to upload the foreignKey Id, not the iterated number that import-export creates.
models.py
from django.db import models
from import_export import resources
class School(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
school_id = models.IntegerField(unique=True)
class Sol(models.Model):
school_id = models.ForeignKey(School, to_field='school_id')
name = models.CharField(max_length = 100)
Al1EOC = models.DecimalField(max_digits=5, decimal_places=2)
AL2EOC = models.DecimalField(max_digits=5, decimal_places=2)
#Class for django-import-export
class SolResource(resources.ModelResource):
class Meta:
model = Sol
My admin.py
from import_export.admin import ImportExportModelAdmin
class SolAdmin(ImportExportModelAdmin):
list_display = ('name', 'school_id')
resources_class = SolResource
pass
admin.site.register(Sol, SolAdmin)
My data.csv
id, name, school_id, Al1EOC, AL2EOC
,'Main st school', 1238008, 12.9, 14.9
When I export the data from the SOL model, I get an iterated number for the school ID. I want the actual School ID - the one that holds the foreignKey relationship. And, subsequently, I need to upload data with that foreignKey number. I know the ForeignKey widget is the way to do it, but I don;t understand how it is implemented.
There is ForeignKeyWidget in the documentation. You can use it here. There are also IntegerWidget and DecimalWidget.
from import_export.admin import ImportExportModelAdmin
class SolResource(resources.ModelResource):
school_id = fields.Field(
column_name='school_id',
attribute='school_id',
widget=ForeignKeyWidget(School, 'name'))
class Meta:
model = Sol
class SolAdmin(ImportExportModelAdmin):
list_display = ('name', 'school_id')
resources_class = SolResource
admin.site.register(Sol, SolAdmin)
This is a working example. Hope it will help.
i think it will help:
class SolResource(resources.ModelResource):
school_id = fields.Field()
class Meta:
# ...
def dehydrate_school_id(self, sol):
return sol.school_id.school_id # You can get all fields of related model. This is example.
Use the widget- works great.
school_id = fields.Field(column_name='school_id', attribute='Sol', widget=widgets.ForeignKeyWidget(Sol, 'school_id'))