'Import file where its column names different from the names in model caused rows with empty string

I would like to use django-import-export package to import some TSV files, but I'm facing some problems.

In the file, I have the column names in uppercase like this: INDUSTRYGROUPTYPE, TOTALOFFERINGAMOUNT, TOTALREMAINING

In the model I have the same name but with lowercase and undersocre like so:

class Offering(models.Model):
    industry_group_type = models.CharField(max_length=260)
    total_offering_amount = models.CharField(max_length=17)
    total_remaining = models.CharField(max_length=17)

The resource class look like this:

class OfferingResource(ModelResource):
    industry_group_type = Field(attribute='INDUSTRYGROUPTYPE')
    total_offering_amount = Field(attribute='TOTALOFFERINGAMOUNT')
    total_remaining = Field(attribute='TOTALREMAINING')

    class Meta:
        model = Offering
        fields = ('id', 'industry_group_type', 'total_offering_amount', 'total_remaining')
        

When the file is imported, empty rows are created that contain just empty string '', and there is no data.



Solution 1:[1]

I found the solution, I should use column names of the file as class attribute variable name, not as the value of the attribute argument of the Field class.

So, the resource class should be like this:

class OfferingResource(ModelResource):
    INDUSTRYGROUPTYPE = Field(attribute='industry_group_type')
    TOTALOFFERINGAMOUNT = Field(attribute='total_offering_amount')
    TOTALREMAINING = Field(attribute='total_remaining')

    class Meta:
        model = Offering
        fields = ('id', 'INDUSTRYGROUPTYPE', 'TOTALOFFERINGAMOUNT', 'TOTALREMAINING')

Solution 2:[2]

As you say, declaring the resource field to match the attribute name will work, but you can also use the column_name parameter to declare the field name in the tsv. You can use 'attribute' to declare the corresponding attribute on your model object

class OfferingResource(ModelResource):
    industry_group_type = Field(attribute='industry_group_type', column_name='INDUSTRYGROUPTYPE')
    total_offering_amount = Field(attribute='total_offering_amount', column_name='TOTALOFFERINGAMOUNT')
    total_remaining = Field(attribute='total_remaining', column_name='TOTALREMAINING')

    class Meta:
        model = Offering
        fields = ('id', 'industry_group_type', 'total_offering_amount', 'total_remaining')

Docs

Solution 3:[3]

try this:

a = int(input('Your first number: '))
b = int(input('Your second number: '))
c = int(input('Your third number: '))

def largest(a, b, c):
    largest = max([a,b,c])
    print('The largest number is: ', largest)

def smallest(a, b, c):
    smallest = min([a,b,c])
    print('The smallest number is: ', smallest)

def average(a, b, c):
    if c<a<b or b<a<c:
        average_num = a
    elif a<c<b or b<c<a:
        average_num = c
    elif c<b<a or a<b<c:
        average_num = b
    else: #else all numbers are equal
        average = a
    print('The average number is: ', average_num)

largest(a, b, c)
average(a, b, c)
smallest(a, b, c)

or you could just do:

a = int(input('Your first number: '))
b = int(input('Your second number: '))
c = int(input('Your third number: '))
lst = [a,b,c]
lst.sort()
print("smallest = %s" % lst[0])
print("average = %s" % lst[1])
print("maximum = %s" % lst[-1])

Solution 4:[4]

elif (b > a) and ( b < c):

This line only works in the case that b is bigger than a and smaller than c.

But it doesn't work if b is bigger than c and smaller than a.

You can try:

elif ((b > a) and ( b < c) or (b < a) and ( b > c)):

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oussama He
Solution 2 Matthew Hegarty
Solution 3
Solution 4 Willow