0

I created a system with Django. In this system, user uploads an excel table and I creating a new customer from that excel. But in this excel I have 2 important columns. They are entity and parent. I want to when a user uploads this excel table but If there is an entity or parent that is not registered in my database, I want to create it and then save it. I user get_or_createe for that but I am getting an error:

ValueError at /customers/upload Cannot assign "(<ParentCompany: TESTP>, False)": "Customer.parent" must be a "ParentCompany" instance.

How can I solve it?

views.py

def customer_excel_upload(request):
    current_user = request.user
    userP = UserProfile.objects.get_or_create(username=current_user)
    company = userP[0].company
    if request.method == 'POST':
        form = CustomerExcelForm(request.POST, request.FILES)
        if form.is_valid():
            new_excel = form.save()
            new_excel = new_excel.excel
            df = pd.read_excel('C:/fray/otc/'
                               +
                               new_excel.name,
                               index_col=0,
                               engine='openpyxl')
            for index, row in df.iterrows():
                if row is not None:
                    new_customer = Customer()
                    new_customer.customer_name = index
                    country = Country.objects.get(country_name=row['Country'])
                    new_customer.address = row['Address']
                    new_customer.customer_number = row['Customer Number']
                    new_customer.phone_number = row['Phone Number']
                    new_customer.email_address = row['Email Adress']
                    new_customer.credit_limit = row['Credit Limit']
                    new_customer.currency_choice = row['Currency choice']
                    new_customer.risk_rating = row['Risk rating']
                    parent = ParentCompany.objects.get_or_create(parent=row['Parent Company'],
                                                                 company=request.user.company)
                    entity = Entities.objects.get_or_create(entities=row['Entity'], company=request.user.company)
                    new_customer.parent = parent
                    new_customer.entity = entity
                    new_customer.country = country
                    new_customer.company = company
                    new_customer.save()
            return redirect('home')
    else:
        form = CustomerExcelForm()

    context = {
        'form': form
    }
    return render(request, 'customer_excel_upload.html', context)

models.py

class Customer(models.Model):
    customer_name = models.CharField(max_length=100)
    country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True, unique=False)
    address = models.CharField(max_length=250),
    ...
    parent = models.ForeignKey(ParentCompany, on_delete=models.CASCADE, blank=True, null=True)
    entity = models.ForeignKey(Entities, on_delete=models.CASCADE, blank=True, null=True)

class ParentCompany(models.Model):
    parent = models.CharField(max_length=50, null=True)
    company = models.ForeignKey(CompanyProfile, on_delete=models.CASCADE, null=True, unique=False)
class Entities(models.Model):
    entities = models.CharField(max_length=250)
    company = models.ForeignKey(CompanyProfile, on_delete=models.CASCADE, null=True, unique=False)

1 Answer 1

1

get_or_create doesn't return an object directly, as the error message says. You can check the docs. It returns a tuple of (object, created).

Yours is returning correctly

(<ParentCompany: TESTP>, False)

You just need to get the first part.

you can change your get_or_create statements to get the first like this:

ParentCompany.objects.get_or_create(parent=row['Parent Company'], company=request.user.company)[0]

note the [0] on the end.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.