=================
scrapy-djangoitem
=================
.. image:: https://img.shields.io/pypi/v/scrapy-djangoitem.svg
:target: https://pypi.python.org/pypi/scrapy-djangoitem
:alt: PyPI Version
.. image:: https://img.shields.io/travis/scrapy-plugins/scrapy-djangoitem/master.svg
:target: http://travis-ci.org/scrapy-plugins/scrapy-djangoitem
:alt: Build Status
.. image:: https://img.shields.io/github/license/scrapy-plugins/scrapy-djangoitem.svg
:target: https://github.com/scrapy-plugins/scrapy-djangoitem/blob/master/LICENSE
:alt: License
``scrapy-djangoitem`` is an extension that allows you to define `Scrapy items
<http://doc.scrapy.org/en/latest/topics/items.html>`_ using existing `Django
models <https://docs.djangoproject.com/en/latest/topics/db/models/>`_.
This utility provides a new class, named ``DjangoItem``, that you can use as a
regular Scrapy item and link it to a Django model with its ``django_model``
attribute. Start using it right away by importing it from this package::
from scrapy_djangoitem import DjangoItem
Installation
============
Starting with ``v1.1`` both ``Python 2.7`` and ``Python 3.4/3.5`` are
supported. For ``Python 3`` you need ``Scrapy v1.1`` or above.
Latest tested Django version is ``Django 1.9``.
Install from ``PyPI`` using::
pip install scrapy-djangoitem
Introduction
============
``DjangoItem`` is a class of item that gets its fields definition from a
Django model, you simply create a ``DjangoItem`` and specify what Django
model it relates to.
Besides of getting the model fields defined on your item, ``DjangoItem``
provides a method to create and populate a Django model instance with the item
data.
Usage
=====
``DjangoItem`` works much like ModelForms in Django, you create a subclass
and define its ``django_model`` attribute to be a valid Django model. With this
you will get an item with a field for each Django model field.
In addition, you can define fields that aren't present in the model and even
override fields that are present in the model defining them in the item.
Let's see some examples:
Creating a Django model for the examples::
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=255)
age = models.IntegerField()
Defining a basic ``DjangoItem``::
from scrapy_djangoitem import DjangoItem
class PersonItem(DjangoItem):
django_model = Person
``DjangoItem`` works just like Scrapy items::
>>> p = PersonItem()
>>> p['name'] = 'John'
>>> p['age'] = '22'
To obtain the Django model from the item, we call the extra method
``DjangoItem.save()`` of the ``DjangoItem``::
>>> person = p.save()
>>> person.name
'John'
>>> person.age
'22'
>>> person.id
1
The model is already saved when we call ``DjangoItem.save()``, we
can prevent this by calling it with ``commit=False``. We can use
``commit=False`` in ``DjangoItem.save()`` method to obtain an unsaved model::
>>> person = p.save(commit=False)
>>> person.name
'John'
>>> person.age
'22'
>>> person.id
None
As said before, we can add other fields to the item::
import scrapy
from scrapy_djangoitem import DjangoItem
class PersonItem(DjangoItem):
django_model = Person
sex = scrapy.Field()
::
>>> p = PersonItem()
>>> p['name'] = 'John'
>>> p['age'] = '22'
>>> p['sex'] = 'M'
And we can override the fields of the model with your own::
class PersonItem(DjangoItem):
django_model = Person
name = scrapy.Field(default='No Name')
This is useful to provide properties to the field, like a default or any other
property that your project uses. Those additional fields won't be taken into
account when doing a ``DjangoItem.save()``.
Caveats
=======
``DjangoItem`` is a rather convenient way to integrate Scrapy projects with Django
models, but bear in mind that Django ORM **may not scale well** if you scrape a lot
of items (ie. millions) with Scrapy. This is because a relational backend is
**often not a good choice for a write intensive applications** (such as a web
crawler), specially if the database is highly normalized and with many indices.
Setup
=====
To use the Django models outside the Django application you need to set up the
``DJANGO_SETTINGS_MODULE`` environment variable and --in most cases-- modify
the ``PYTHONPATH`` environment variable to be able to import the settings
module.
There are many ways to do this depending on your use case and preferences.
Below is detailed one of the simplest ways to do it.
Suppose your Django project is named ``mysite``, is located in the path
``/home/projects/mysite`` and you have created an app ``myapp`` with the model
``Person``. That means your directory structure is something like this::
/home/projects/mysite
├── manage.py
├── myapp
│ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
Then you need to add ``/home/projects/mysite`` to the ``PYTHONPATH``
environment variable and set up the environment variable
``DJANGO_SETTINGS_MODULE`` to ``mysite.settings``. That can be done in your
Scrapy's settings file by adding the lines below::
import sys
sys.path.append('/home/projects/mysite')
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
Notice that we modify the ``sys.path`` variable instead the ``PYTHONPATH``
environment variable as we are already within the python runtime. If everything
is right, you should be able to start the ``scrapy shell`` command and import
the model ``Person`` (i.e. ``from myapp.models import Person``).
Starting with ``Django 1.8`` you also have to explicitly set up ``Django`` if using
it outside a ``manage.py`` context
(see `Django Docs <https://docs.djangoproject.com/en/1.8/intro/tutorial01/#playing-with-the-api>`_)::
import django
django.setup()
Development
===========
Test suite from the ``tests`` directory can be run using ``tox`` by running::
tox
...using the configuration in ``tox.ini``. The ``Python`` interpreters
used have to be installed locally on the system.
Changelog
=========
v1.1.1 (2016-05-04)
-------------------
* Distribute as universal wheel
* Fix README's markup
v1.1 (2016-05-04)
-----------------
* ``Python 3.4/3.5`` support
* Making tests work with ``Django 1.9`` again
v1.0 (2015-04-29)
-----------------
* Initial version
Raw data
{
"_id": null,
"home_page": "https://github.com/scrapy-plugins/scrapy-djangoitem",
"name": "scrapy-djangoitem",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Scrapy developers",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/50/10/7b8d1caf3c292069b1bc983c2c872597f429bac4673707d11ee76313515a/scrapy-djangoitem-1.1.1.tar.gz",
"platform": "UNKNOWN",
"description": "=================\nscrapy-djangoitem\n=================\n\n.. image:: https://img.shields.io/pypi/v/scrapy-djangoitem.svg\n :target: https://pypi.python.org/pypi/scrapy-djangoitem\n :alt: PyPI Version\n\n.. image:: https://img.shields.io/travis/scrapy-plugins/scrapy-djangoitem/master.svg\n :target: http://travis-ci.org/scrapy-plugins/scrapy-djangoitem\n :alt: Build Status\n\n.. image:: https://img.shields.io/github/license/scrapy-plugins/scrapy-djangoitem.svg\n :target: https://github.com/scrapy-plugins/scrapy-djangoitem/blob/master/LICENSE\n :alt: License\n\n\n``scrapy-djangoitem`` is an extension that allows you to define `Scrapy items\n<http://doc.scrapy.org/en/latest/topics/items.html>`_ using existing `Django\nmodels <https://docs.djangoproject.com/en/latest/topics/db/models/>`_.\n\nThis utility provides a new class, named ``DjangoItem``, that you can use as a\nregular Scrapy item and link it to a Django model with its ``django_model``\nattribute. Start using it right away by importing it from this package::\n\n from scrapy_djangoitem import DjangoItem\n\nInstallation\n============\n\nStarting with ``v1.1`` both ``Python 2.7`` and ``Python 3.4/3.5`` are\nsupported. For ``Python 3`` you need ``Scrapy v1.1`` or above.\n\nLatest tested Django version is ``Django 1.9``.\n\nInstall from ``PyPI`` using::\n\n pip install scrapy-djangoitem\n\n\nIntroduction\n============\n\n``DjangoItem`` is a class of item that gets its fields definition from a\nDjango model, you simply create a ``DjangoItem`` and specify what Django\nmodel it relates to.\n\nBesides of getting the model fields defined on your item, ``DjangoItem``\nprovides a method to create and populate a Django model instance with the item\ndata.\n\nUsage\n=====\n\n``DjangoItem`` works much like ModelForms in Django, you create a subclass\nand define its ``django_model`` attribute to be a valid Django model. With this\nyou will get an item with a field for each Django model field.\n\nIn addition, you can define fields that aren't present in the model and even\noverride fields that are present in the model defining them in the item.\n\nLet's see some examples:\n\nCreating a Django model for the examples::\n\n from django.db import models\n\n class Person(models.Model):\n name = models.CharField(max_length=255)\n age = models.IntegerField()\n\nDefining a basic ``DjangoItem``::\n\n from scrapy_djangoitem import DjangoItem\n\n class PersonItem(DjangoItem):\n django_model = Person\n\n``DjangoItem`` works just like Scrapy items::\n\n >>> p = PersonItem()\n >>> p['name'] = 'John'\n >>> p['age'] = '22'\n\nTo obtain the Django model from the item, we call the extra method\n``DjangoItem.save()`` of the ``DjangoItem``::\n\n >>> person = p.save()\n >>> person.name\n 'John'\n >>> person.age\n '22'\n >>> person.id\n 1\n\nThe model is already saved when we call ``DjangoItem.save()``, we\ncan prevent this by calling it with ``commit=False``. We can use\n``commit=False`` in ``DjangoItem.save()`` method to obtain an unsaved model::\n\n >>> person = p.save(commit=False)\n >>> person.name\n 'John'\n >>> person.age\n '22'\n >>> person.id\n None\n\nAs said before, we can add other fields to the item::\n\n import scrapy\n from scrapy_djangoitem import DjangoItem\n\n class PersonItem(DjangoItem):\n django_model = Person\n sex = scrapy.Field()\n\n::\n\n >>> p = PersonItem()\n >>> p['name'] = 'John'\n >>> p['age'] = '22'\n >>> p['sex'] = 'M'\n\nAnd we can override the fields of the model with your own::\n\n class PersonItem(DjangoItem):\n django_model = Person\n name = scrapy.Field(default='No Name')\n\nThis is useful to provide properties to the field, like a default or any other\nproperty that your project uses. Those additional fields won't be taken into\naccount when doing a ``DjangoItem.save()``.\n\nCaveats\n=======\n\n``DjangoItem`` is a rather convenient way to integrate Scrapy projects with Django\nmodels, but bear in mind that Django ORM **may not scale well** if you scrape a lot\nof items (ie. millions) with Scrapy. This is because a relational backend is\n**often not a good choice for a write intensive applications** (such as a web\ncrawler), specially if the database is highly normalized and with many indices.\n\nSetup\n=====\n\nTo use the Django models outside the Django application you need to set up the\n``DJANGO_SETTINGS_MODULE`` environment variable and --in most cases-- modify\nthe ``PYTHONPATH`` environment variable to be able to import the settings\nmodule.\n\nThere are many ways to do this depending on your use case and preferences.\nBelow is detailed one of the simplest ways to do it.\n\nSuppose your Django project is named ``mysite``, is located in the path\n``/home/projects/mysite`` and you have created an app ``myapp`` with the model\n``Person``. That means your directory structure is something like this::\n\n /home/projects/mysite\n \u251c\u2500\u2500 manage.py\n \u251c\u2500\u2500 myapp\n \u2502\u00a0\u00a0 \u251c\u2500\u2500 __init__.py\n \u2502\u00a0\u00a0 \u251c\u2500\u2500 models.py\n \u2502\u00a0\u00a0 \u251c\u2500\u2500 tests.py\n \u2502\u00a0\u00a0 \u2514\u2500\u2500 views.py\n \u2514\u2500\u2500 mysite\n \u251c\u2500\u2500 __init__.py\n \u251c\u2500\u2500 settings.py\n \u251c\u2500\u2500 urls.py\n \u2514\u2500\u2500 wsgi.py\n\nThen you need to add ``/home/projects/mysite`` to the ``PYTHONPATH``\nenvironment variable and set up the environment variable\n``DJANGO_SETTINGS_MODULE`` to ``mysite.settings``. That can be done in your\nScrapy's settings file by adding the lines below::\n\n import sys\n sys.path.append('/home/projects/mysite')\n\n import os\n os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'\n\nNotice that we modify the ``sys.path`` variable instead the ``PYTHONPATH``\nenvironment variable as we are already within the python runtime. If everything\nis right, you should be able to start the ``scrapy shell`` command and import\nthe model ``Person`` (i.e. ``from myapp.models import Person``).\n\nStarting with ``Django 1.8`` you also have to explicitly set up ``Django`` if using\nit outside a ``manage.py`` context\n(see `Django Docs <https://docs.djangoproject.com/en/1.8/intro/tutorial01/#playing-with-the-api>`_)::\n\n import django\n django.setup()\n\n\nDevelopment\n===========\n\nTest suite from the ``tests`` directory can be run using ``tox`` by running::\n\n tox\n\n...using the configuration in ``tox.ini``. The ``Python`` interpreters\nused have to be installed locally on the system.\n\n\nChangelog\n=========\n\nv1.1.1 (2016-05-04)\n-------------------\n\n* Distribute as universal wheel\n* Fix README's markup\n\nv1.1 (2016-05-04)\n-----------------\n\n* ``Python 3.4/3.5`` support\n* Making tests work with ``Django 1.9`` again\n\nv1.0 (2015-04-29)\n-----------------\n\n* Initial version",
"bugtrack_url": null,
"license": "BSD",
"summary": "Scrapy extension to write scraped items using Django models",
"version": "1.1.1",
"project_urls": {
"Homepage": "https://github.com/scrapy-plugins/scrapy-djangoitem"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5cffca88e5fa4b5df11876d877c568de8f5e1a06587bcd23dab7d1847a9b1123",
"md5": "85e5614159e8164e7fd434a8c95668ea",
"sha256": "58da093778f2cd92734b5539adcb60d59854475050d7511c42173bd8c104acef"
},
"downloads": -1,
"filename": "scrapy_djangoitem-1.1.1-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "85e5614159e8164e7fd434a8c95668ea",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 8293,
"upload_time": "2016-05-04T16:45:10",
"upload_time_iso_8601": "2016-05-04T16:45:10.495250Z",
"url": "https://files.pythonhosted.org/packages/5c/ff/ca88e5fa4b5df11876d877c568de8f5e1a06587bcd23dab7d1847a9b1123/scrapy_djangoitem-1.1.1-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "50107b8d1caf3c292069b1bc983c2c872597f429bac4673707d11ee76313515a",
"md5": "fbd7c04100fd4e95d7ea43e99ab7ba40",
"sha256": "2c633ae1559f6e3d43c9a5ebb19aa5a64152deef434aa3fa57168e6b002d9c50"
},
"downloads": -1,
"filename": "scrapy-djangoitem-1.1.1.tar.gz",
"has_sig": false,
"md5_digest": "fbd7c04100fd4e95d7ea43e99ab7ba40",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5223,
"upload_time": "2016-05-04T16:45:19",
"upload_time_iso_8601": "2016-05-04T16:45:19.212651Z",
"url": "https://files.pythonhosted.org/packages/50/10/7b8d1caf3c292069b1bc983c2c872597f429bac4673707d11ee76313515a/scrapy-djangoitem-1.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2016-05-04 16:45:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "scrapy-plugins",
"github_project": "scrapy-djangoitem",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"requirements": [],
"tox": true,
"lcname": "scrapy-djangoitem"
}