简体   繁体   中英

Importing many to many relationship (Django)

I want to import data to many to many fields.

My code below imports the data, but only applies one relationship to the model (not two, as I've asked it to do below by importing it twice- where I wrote "cat = ...")

In my example below, I want it to import cat (category) from columns 4 & 11. My code below only applies one category to the model (not both).

How can I get it to apply both fields to the model? Im using python 2.7

import csv

l = list(csv.reader(open('test_data.csv', 'rb')))

Gender_CHOICES = {
    'Male': 1,
    'Female': 2,
    'Unisex': 3,
}

Stock_CHOICES = {
    'in stock': 1,
    'low stock': 2,
    'out of stock': 3,
    'discountinued': 4
}

for i in l[1:]:
        cat = m.Category.objects.get_or_create(category_name = i[4],[11])[0]
        prod = m.Product(
            name = i[0],
            link = i[1],
            description = i[6],
            brand = i[7],
            colour = i[10],
            gender = Gender_CHOICES[i[8]] if i[8] in Gender_CHOICES else 3,
            store = m.Store.objects.get_or_create(store_name = i[2])[0]
            )
        prod.save()
        var = m.Variation(
            product = prod,
            variation = "default"
            )
        var.save()
        img = m.Image(
            variation = var,
            image = i[5]
            )
        img.save()
        size = m.Size(
            variation = var
            )
        size.save()
        price = m.Price(
            variation = var,
            price = float(i[3])
            )
        price.save()
        stock = m.Stock(
            size = size,
            stock = Stock_CHOICES[i[9]] if i[9] in Stock_CHOICES else 4
            )
        stock.save()
        prod.category.add(cat)

Sample of CSV:

prod_name,prod_link,store_name,prod_price,category,image_default,prod_description,prod_brand,gender,prod_stock,category1
Bliss Firm Baby Firm Lifting & Volumising Serum 30ml - Serum,http://click.linksynergy.com/link?id=dnw*50nuNL8&offerid=287549.2554637&type=15&murl=http%3A%2F%2Fwww.asos.com%2Fau%2FBliss%2FBliss-Firm-Baby-Firm-Lifting-Volumising-Serum-30ml%2FProd%2Fpgeproduct.aspx%3Fiid%3D3936070%26istCompanyId%3Df448b47d-6b90-4b9b-a52d-eb6058c99b1c%26istItemId%3Dwxqqpxxmi%26istBid%3Dt,Asos,117,Skin Care Body Creams & Moisturisers,http://images.asos-media.com/inv/media/0/7/0/6/3936070/serum/image1xxl.jpg,Firm Baby Firm Lifting & Volumising Serum by Bliss Designed to boost collagen and elasticity Concentrated formula with a water-free aloe base Aims to plump skin from the inside out,Bliss,Female,
Yes To Carrots Day Cream 50ml - Carrots,http://click.linksynergy.com/link?id=dnw*50nuNL8&offerid=287549.2825448&type=15&murl=http%3A%2F%2Fwww.asos.com%2Fau%2FYES-TO%2FYes-To-Carrots-Day-Cream-50ml%2FProd%2Fpgeproduct.aspx%3Fiid%3D4254119%26istCompanyId%3Df448b47d-6b90-4b9b-a52d-eb6058c99b1c%26istItemId%3Dwiqwwawpm%26istBid%3Dt,Asos,21,Skin Care Body Creams & Moisturisers,http://images.asos-media.com/inv/media/9/1/1/4/4254119/carrots/image1xxl.jpg,Day cream by Yes To Carrots 95% natural ingredients Including carrots and sweet almond oil Rich moisturising formula Naturally nourishes to promote softer skin Suitable for normal to dry skin types Product size: 50ml,YES TO,Female,Belts

在此处输入图片说明

I believe, the problem is with Object creation. I think the following is what you are looking for :

....
....
for i in l[1:]:
        cat_1 = m.Category.objects.get_or_create(category_name = i[4])
        cat_2 = m.Category.objects.get_or_create(category_name = i[11])
....
....
prod.category.add(cat_1)
prod.category.add(cat_2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM