Skip to content

[WIP] allow foreign key support #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions scrapy_djangoitem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
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_fields = {}
cls._model_meta = cls.django_model._meta
for model_field in cls._model_meta.fields:
if not model_field.auto_created:
if model_field.name not in cls.fields:
cls.fields[model_field.name] = Field()
cls._model_fields.append(model_field.name)
cls._model_fields[model_field.name] = model_field
return cls


Expand Down Expand Up @@ -67,7 +66,12 @@ def _get_errors(self, exclude=None):
@property
def instance(self):
if self._instance is None:
modelargs = dict((k, self.get(k)) for k in self._values
if k in self._model_fields)
modelargs = {}
for k in self._values:
if k in self._model_fields:
if self._model_fields[k].is_relation:
modelargs[k] = self._model_fields[k].related_model(pk=self.get(k))
else:
modelargs[k] = self.get(k)
self._instance = self.django_model(**modelargs)
return self._instance
9 changes: 9 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ class IdentifiedPerson(models.Model):

class Meta:
app_label = 'test_djangoitem'


class Property(models.Model):
person = models.ForeignKey(Person)
name = models.CharField(max_length=255)
description = models.TextField()

class Meta:
app_label = 'test_djangoitem'
14 changes: 13 additions & 1 deletion tests/test_djangoitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
django.setup()

from scrapy_djangoitem import DjangoItem, Field
from tests.models import Person, IdentifiedPerson
from tests.models import Person, IdentifiedPerson, Property


class BasePersonItem(DjangoItem):
Expand All @@ -25,6 +25,10 @@ class IdentifiedPersonItem(DjangoItem):
django_model = IdentifiedPerson


class PropertyItem(DjangoItem):
django_model = Property


class DjangoItemTest(unittest.TestCase):

def assertSortedEqual(self, first, second, msg=None):
Expand Down Expand Up @@ -100,3 +104,11 @@ def test_default_field_values(self):
i = BasePersonItem()
person = i.save(commit=False)
self.assertEqual(person.name, 'Robot')

def test_foreign_key(self):
i = PropertyItem()
i['name'] = 'White House'
i['description'] = 'White House'
i['person'] = 1
p = i.save(commit=False)
self.assertTrue(p)