django models manager for content type models
<p>I am currently writing a Django Application to contain all the Content Type Models(generic relations)</p>

<pre><code>from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.validators import RegexValidator

class AddressManager(models.Manager) :
    def create_address(self, content_obj, address1, address2, postal_code):
        address = Address(content_object = content_obj,
                          address1 = address1,
                          address2 = address2,
                          postal_code = postal_code)
        address.save()
        return address

class Address(models.Model):
    address1 = models.CharField(max_length=100, default=None)
    address2 = models.CharField(max_length=100, default=None)
    postal_code = models.TextField(default=None)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE,
                                     default=None, null=True)
    object_id = models.PositiveIntegerField(default=None, null=True)
    content_object = GenericForeignKey('content_type', 'object_id')

    objects = AddressManager()
</code></pre>

<p>When I use this on local server, it serves its purpose well.</p>

<p>However, when I run</p>

<p>python manage.py test</p>

<p>This error occurs</p>

<pre><code>======================================================================
ERROR: test_address__custom_methods (genericmodels.tests.test_models.TestBasic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path/genericmodels/tests/test_models.py", line 15, in test_address__custom_methods
    "t_postal_code")
  File "/path/genericmodels/models.py", line 11, in create_address
    postal_code = postal_code)
  File "/path/env3/lib/python3.5/site-packages/django/db/models/base.py", line 555, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'content_object' is an invalid keyword argument for this function

----------------------------------------------------------------------
Ran 1 test in 0.310s

FAILED (errors=1)
Destroying test database for alias 'default'...
</code></pre>

<p>The test code is as follows :</p>

<pre><code>from django.test import TestCase
from unittest.mock import MagicMock

from genericmodels.models import *
from anotherapp.models import User_Info


class TestBasic(TestCase):
    """ Basic tests for Address model"""
    def test_address__custom_methods(self):
        user_info = MagicMock(User_Info)
        address = Address.objects.create_address(user_info,
                                                 "t_address1",
                                                 "t_address2",
                                                 "t_postal_code")
        self.assertEqual(Address.objects.count(), 1)
        self.assertEqual(address.address1, "t_address1")
</code></pre>

<p>Thanks to <a href="http://stackoverflow.com/users/5662309/ticktomp">TickTomp</a> I was able to figure out that instead of making a MagicMock(User_Info), I should have actually made an instance of User_Info class to test. The test ran fine afterwards...</p>

python django django-models django-testing