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'))
Related
I am facing problems with importing csv file into my db in django.
I don't want to add id field when I import the csv file.
app.admin/
from django.contrib import admin
from .models import Products, Forimg
from import_export.admin import ImportExportModelAdmin
#admin.register(Products, Forimg)
class ViewAdmin(ImportExportModelAdmin):
pass
app.models/
class Products(models.Model):
Code = models.CharField(max_length=10)
Product_description = models.CharField(max_length=200,primary_key = True)
Val_tech = models.CharField(max_length=5)
Quantity = models.IntegerField(default=0)
UOM = models.CharField(max_length=5)
Rate = models.FloatField(default=0)
Value = models.FloatField(default=0)
def __str__(self):
return self.Product_description
class Forimg(models.Model):
products = models.OneToOneField(Products, on_delete=models.CASCADE, primary_key = True)
image = models.ImageField(upload_to = 'img/', width_field= 30, height_field = 40)
def __str__(self):
return self.products.Product_description
To ignore the id field during import, you can configure your Resource to exclude that field:
class YourResource(resources.ModelResource):
class Meta:
model = YourModel
exclude = ('id', )
To make the id field always start at 1, then you would need to include the id field in your csv file:
id,name
1,name1
2,name2
Then ensure that these fields are imported. Note that exclude is not used here:
class YourResource(resources.ModelResource):
class Meta:
model = YourModel
include = ('id','name')
If you re-import the same file, then you need to make sure that any existing rows are updated (or skipped). To achieve this, add logic to identify the import as an 'update'. To have the row skipped (instead of updated), enable skip_unchanged:
class YourResource(resources.ModelResource):
class Meta:
model = YourModel
include = ('id','name')
import_id_fields = ('id',)
skip_unchanged = True
I hope this helps to solve your issue. If not, please update your question and be clearer about your issue.
The requirement is "I want to insert person with the person groups selection and also at the time of Creating person group I can choose persons for that particular group".
I've added two models in my models.py and manage many to many relationship between.
models.py
from django.db import models
class PersonGroup(models.Model):
id = models.AutoField(primary_key=True)
groupName = models.CharField(max_length=30)
detail = models.CharField(max_length=200)
class Person(models.Model):
id = models.AutoField(primary_key=True)
personId = models.CharField(max_length=20)
personName = models.CharField(max_length=20)
state = models.IntegerField()
personGroup = models.ManyToManyField(PersonGroup, related_name="person_list", blank=True)
serializers.py
class PersonSerializer(serializers.ModelSerializer):
personGroup = serializers.PrimaryKeyRelatedField(queryset=PersonGroup.objects.all(), many=True)
class Meta:
model = Person
fields = '__all__'
class PersonGroupSerializer(serializers.ModelSerializer):
person_list = PersonSerializer(many=True, read_only=True)
class Meta:
model = PersonGroup
fields = '__all__'
The above code help me to create person with personGroup selection
But, I also want to add persons selection at the time of create personGroup. Currently at the time of creating personGroup I'm not allowed to enter persons.
Please let me know if there any solution by which I can also select available persons at the time of person group creation.
Your person_list field in the PersonGroupSerializer is on read only, so you can't modify it using the API.
person_list = serializers.PrimaryKeyRelatedField(queryset=Person.objects.all(), many=True)
Try removing this arg.
You might also want to switch to a ForeignKey field instead of slugged.
In my database, I store the file name of a particular image for an item. Let's say this is the model in models.py
from django.db import models
class Product(models.Model):
sku = models.CharField(validators=[isalphanumeric], max_length=20, null=False, blank=False)
image = models.CharField(max_length=20, blank=False, null=False)
and then I have a serializer defined like so in serializers.py
from rest_framework import serializers
from app.models import Product
class ProductSerializer(serializer.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
what I want is to be able to add a string to the image of a Product that makes it into a string representing the relative link, something like:
storage_location = '/assets/img'
img_url = f'{storage_location}/{image}'
The reason why I want to do this is because I want to be flexible with the urls rather than having the file name be a "file location" and then having to update the database each time I change how I arrange my images (I'm still not sure how to store them).
How can I do that?
First of all you can use model's ImageField for this:
class Product(models.Model):
sku = models.CharField(validators=[isalphanumeric], max_length=20, null=False, blank=False)
image = models.ImageField(max_length=20, blank=False)
This will automatically add MEDIA_URL setting to the value when you fetch value.
In case you want to use CharField you can do what you need on serializer level using SerializerMethodField:
class ProductSerializer(serializer.ModelSerializer):
image = serializers.SerializerMethodField()
def get_image(self, obj):
storage_location = '/assets/img'
img_url = f'{storage_location}/{obj.image}'
return img_url
class Meta:
model = Product
fields = '__all__'
Try following in your serialiser.py
class ProductSerializer(serializer.ModelSerializer):
img_url = serializers.SerializerMethodField()
class Meta:
model = Product
fields = '__all__'
#fields = ('sku', 'img_url') # This is what i will prefer
def get_img_url(self, obj):
storage_location = '/assets/img'
img_url = f'{storage_location}/{obj.image}'
return img_url
Good Luck!
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 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]