Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

from scrapy.item import Field, Item, ItemMeta 

 

 

class DjangoItemMeta(ItemMeta): 

 

    def  __new__(mcs, class_name, bases, attrs): 

        cls = super(DjangoItemMeta, mcs).__new__(mcs, class_name, bases, attrs) 

        cls.fields = cls.fields.copy() 

 

        if cls.django_model: 

            cls._model_fields = [] 

            cls._model_meta = cls.django_model._meta 

            for model_field in cls._model_meta.fields: 

                if model_field.auto_created == False: 

                    if model_field.name not in cls.fields: 

                        cls.fields[model_field.name] = Field() 

                    cls._model_fields.append(model_field.name) 

        return cls 

 

 

class DjangoItem(Item): 

 

    __metaclass__ = DjangoItemMeta 

 

    django_model = None 

 

    def save(self, commit=True): 

        modelargs = dict((f, self.get(f, None)) for f in self._model_fields) 

        model = self.django_model(**modelargs) 

31        if commit: 

            model.save() 

        return model