django unit test raise AssertionError for "blank=False" field - python

I have a model field namely descrition and the constraints for the field is
description = models.TextField(
verbose_name=_('Description'),
help_text=_('Description of the requirement'),
blank=True, default=''
)
so it can not be blank from the form level.But i want to write a unit test for this and i have write one and its raising the error AssertionError: ValidationError not raised
My test code(please avoid the other fields and methods,i didn't mention those fields and methods in detail because they are not relevant at this moment)
class RequirementHeaderTestCase(TestCase):
def setUp(self):
self.user = Customer.objects.create_user(email="user#user.com")
self.user_id = self.user.id
self.anonymous_user_email_id = None
self.session = None
self.description = 'Test Description'
self.requirement_status = 'Complete'
self.service = RequirementService()
def test_requirement_header_without_description(self):
with self.assertRaises(ValidationError) as ex:
self.service.create_requirement_header(
user_id=self.user_id,
anonymous_user_email_id=self.anonymous_user_email_id,
session=self.session,
requirement_status=self.requirement_status,
)
self.assertEqual(ex.exception.__class__, ValidationError)
after run this test, its showing the followin error,
FAIL: test_requirement_header_without_description (requirements.tests.RequirementHeaderTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/equirements/tests.py", line 25, in test_requirement_header_without_description
requirement_status=self.requirement_status,
AssertionError: ValidationError not raised
my question ,how can i write a unit test for a field which is blank=False from the form level.
UPDATE
Here is the relevant method
def create_requirement_header(self, user_id=None,anonymous_user_email_id=None,session=None,description='',requirement_status=None):
# getting Customer object from id
user = None
if user_id is not None:
try:
user = Customer.objects.get(pk=user_id)
except Customer.DoesNotExist:
raise ObjectDoesNotExist('User was not found')
# getting AnonymousUserEmail object from id
anonymous_user_email = None
if anonymous_user_email_id is not None:
try:
anonymous_user_email = AnonymousUserEmail.objects.get(pk=anonymous_user_email_id)
except AnonymousUserEmail.DoesNotExist:
raise ObjectDoesNotExist('AnonymousUserEmail object was not found') # Need to re-think about message
requirement_header = RequirementHeader.objects.create_requirement_header(user=user,
anonymous_email=anonymous_user_email,
session=session,
description=description,
requirement_status=requirement_status)
return {
"id": requirement_header.id,
"description": requirement_header.description,
"status": requirement_header.requirement_status
}

Related

Django testing fails with object not found in response.context even though it works when actually running

I'm trying to test if my PlayerPoint model can give me the top 5 players in regards to their points.
This is the Player model:
class Player(AbstractUser):
phone_number = models.CharField(
max_length=14,
unique=True,
help_text="Please ensure +251 is included"
)
and this is the PlayerPoint model:
class PlayerPoint(models.Model):
OPERATION_CHOICES = (('ADD', 'ADD'), ('SUB', 'SUBTRACT'), ('RMN', 'REMAIN'))
points = models.IntegerField(null=False, default=0)
operation = models.CharField(
max_length=3,
null=False,
choices=OPERATION_CHOICES,
default=OPERATION_CHOICES[2][0]
)
operation_amount = models.IntegerField(null=False)
operation_reason = models.CharField(null=False, max_length=1500)
player = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=False,
on_delete=models.PROTECT,
to_field="phone_number",
related_name="player_points"
)
points_ts = models.DateTimeField(auto_now_add=True, null=False)
class Meta:
get_latest_by = ['pk', 'points_ts']
I also have a pre-save signal handler:
#receiver(signals.pre_save, sender=PlayerPoint)
def pre_save_PlayerPoint(sender, instance, **_):
if sender is PlayerPoint:
try:
current_point = PlayerPoint.objects.filter(player=instance.player).latest()
except PlayerPoint.DoesNotExist as pdne:
if "new player" in instance.operation_reason.lower():
print(f"{pdne} {instance.player} must be a new")
instance.operation_amount = 100
instance.points = int(instance.points) + int(instance.operation_amount)
else:
raise pdne
except Exception as e:
print(f"{e} while trying to get current_point of the player, stopping execution")
raise e
else:
if instance.operation == PlayerPoint.OPERATION_CHOICES[0][0]:
instance.points = int(current_point.points) + int(instance.operation_amount)
elif instance.operation == PlayerPoint.OPERATION_CHOICES[1][0]:
if int(current_point.points) < int(instance.operation_amount):
raise ValidationError(
message="not enough points",
params={"points": current_point.points},
code="invalid"
)
instance.points = int(current_point.points) - int(instance.operation_amount)
As you can see there is a foreign key relation.
Before running the tests, in the setUp() I create points for all the players as such:
class Top5PlayersViewTestCase(TestCase):
def setUp(self) -> None:
self.player_model = get_user_model()
self.test_client = Client(raise_request_exception=True)
self.player_list = list()
for i in range(0, 10):
x = self.player_model.objects.create_user(
phone_number=f"+2517{i}{i}{i}{i}{i}{i}{i}{i}",
# first_name="test",
# father_name="user",
# grandfather_name="tokko",
# email=f"test_user#tokko7{i}.com",
# age="29",
password="password"
)
PlayerPoint.objects.create(
operation="ADD",
operation_reason="new player",
player=x
)
self.player_list.append(x)
counter = 500
for player in self.player_list:
counter += int(player.phone_number[-1:])
PlayerPoint.objects.create(
operation="ADD",
operation_amount=counter,
operation_reason="add for testing",
player=player
)
PlayerPoint.objects.create(
operation="ADD",
operation_amount=counter,
operation_reason="add for testing",
player=player
)
return super().setUp()
def test_monthly_awarding_view_displays_top5_players(self):
for player in self.player_list:
print(player.player_points.latest())
# self.test_client.post("/accounts/login/", self.test_login_success_data)
test_results = self.test_client.get("/points_app/monthly_award/", follow=True)
self.assertEqual(test_results.status_code, 200)
self.assertTemplateUsed(test_results, "points_app/monthlytop5players.html")
self.assertEqual(len(test_results.context.get('results')), 5)
top_5 = PlayerPoint.objects.order_by('-points')[:5]
for pt in top_5:
self.assertIn(pt, test_results.context.get('results'))
The full traceback is this after running coverage run manage.py test points_app.tests.test_views.MonthlyAwardingViewTestCase.test_monthly_awarding_view_displays_top5_players -v 2:
Traceback (most recent call last):
File "/home/gadd/vscodeworkspace/websites/25X/twenty_five_X/points_app/tests/test_views.py", line 358, in test_monthly_awarding_view_displays_top5_players
self.assertIn(pt, test_results.context.get('results'))
AssertionError: <PlayerPoint: 1190 -- +251799999999> not found in [<User25X: +251700000000>, <User25X: +251711111111>, <User25X: +251722222222>, <User25X: +251733333333>, <User25X: +251744444444>]
This is the view being tested:
def get(request):
all_players = get_user_model().objects.filter(is_staff=False).prefetch_related('player_points')
top_5 = list()
for player in all_players:
try:
latest_points = player.player_points.latest()
except Exception as e:
print(f"{player} -- {e}")
messages.error(request, f"{player} {e}")
else:
if all(
[
latest_points.points >= 500,
latest_points.points_ts.year == current_year,
latest_points.points_ts.month == current_month
]
):
top_5.append(player)
return render(request, "points_app/monthlytop5players.html", {"results": top_5[:5]})
What am I doing wrong?
There are 2 problems.
In your view, top_5 is an unsorted list of Player objects.
top_5 = sorted(top_5, key=lambda player: player.player_points.latest().points, reverse=True)[:5] # Add this
return render(request, "points_app/monthlytop5players.html", {"results": top_5[:5]})
In your test, top_5 is a list (actually QuerySet) of PlayerPoint objects.
results_pts = [player.player_points.latest() for player in test_results.context['results']] # Add this
for pt in top_5:
# self.assertIn(pt, test_results.context.get('results')) # Change this
self.assertIn(pt, results_pts) # to this
I think your problem is with this line:
latest_points = player.player_points.latest()
Specifically, latest(). Like get(), earliest() and latest() raise DoesNotExist if there is no object with the given parameters.
You may need to add get_latest_by to your model's Meta class. Maybe try this:
class PlayerPoint(models.Model):
...
class Meta:
get_latest_by = ['joined_ts']
If you don't want to add this to your model, you could just do it directly:
latest_points = player.player_points.latest('-joined_ts')
if this is the problem.

avoid error: 'NoneType' object has no attribute in django model admin

I need to show if a client is having a related contract active or no in customized field
in django model admin
I tried to use try: but it does not work unfortunately
here the original code:
class ClientAdmin(ExportModelAdminMixin, ModelAdmin):
model = Client
menu_icon = "pick"
menu_order = 100
exclude_from_explorer = False
list_display = ["name", "is_active"]
search_fields = ["name"]
list_filter = [ClientFilter]
index_template_name = "wagtailadmin/export_csv.html"
csv_export_fields = ["name"]
def is_active(self, client: Client) -> bool:
any(site.current_contract.is_active for site in client.sites.all())
is_active.boolean = True # type: ignore
is_active.short_description = _("active confirmation") # type: ignore
I got the error:
File "/home/oladhari/reachat-v4/Reachat-v4/backend/crm/admin.py", line 61, in is_active
any(site.current_contract.is_active for site in client.sites.all())
File "/home/oladhari/reachat-v4/Reachat-v4/backend/crm/admin.py", line 61, in <genexpr>
any(site.current_contract.is_active for site in client.sites.all())
AttributeError: 'NoneType' object has no attribute 'is_active'
to resolve this error I tried to use try:
and changed the customized field to this:
def is_active(self, client: Client) -> bool:
try:
any(site.current_contract.is_active for site in client.sites.all())
except ObjectDoesNotExist:
return False
else:
return any(site.current_contract.is_active for site in client.sites.all())
but still having the same error unfortunately
please could you help me to avoid this error, thank you
Why not use a normal query for the is_active method?
So, something like:
def is_active(self, client: Client) -> bool:
return client.sites.filter(current_contract__is_active=True).exists()

Django - AttributeError: 'tuple' object has no attribute 'get' [ DRF, Stripe, Python ]

I constantly get an error whenever I make a POST request to /api/tickets/:
Internal Server Error: /payment-stripe/
Traceback (most recent call last):
File "/ENV/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/ENV/lib/python3.8/site-packages/django/utils/deprecation.py", line 96, in __call__
response = self.process_response(request, response)
File "/ENV/lib/python3.8/site-packages/django/middleware/clickjacking.py", line 26, in process_response
if response.get('X-Frame-Options') is not None:
AttributeError: 'tuple' object has no attribute 'get'
[03/Jan/2020 03:17:09] "POST /payment-stripe/ HTTP/1.1" 500 58576
Here is my stripe payment handling view that I think is causing the problem, but I can't find out why:
#require_http_methods(["POST"])
#csrf_exempt
def stripe_payment_handling(request):
"""
The following code is copied from Stripe Docs.
Everything inside the IF statements is how the ticket gets handled.
"""
payload = request.body
# Checks for the signature of stripe.
sig_header = request.headers['Stripe-Signature']
event = None
try:
# Tests if it can create a connection with Stripe.
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
except ValueError as e:
# invalid payload: If it doesn't pass it gives back a 400
return "Invalid payload", 400
except stripe.error.SignatureVerificationError as e:
# invalid signature: If the Stripe Signature is not there it wont accept it.
return "Invalid signature", 400
# Create the a dictionary with the data
event_dict = event.to_dict()
print(event_dict)
if event_dict['type'] == "payment_intent.created":
# If someone redirects to the bank to pay it will create a payment intent
# Now it will use that value to validate the payment
intent = event_dict['data']['object']
ticket = Ticket.objects.get(
stripe_payment_intent=intent["id"]
)
ticket.status = "Created"
ticket.save()
elif event_dict['type'] == "payment_intent.succeeded":
# If the payment Succeeded:
intent = event_dict['data']['object']
# get the ticket with the intent id that was created from payment_intent.created
ticket = Ticket.objects.get(stripe_payment_intent=intent["id"])
# Change status to Completed
ticket.status = "Completed"
ticket.save()
# Reduce the amount of stock for the selected parking location
ticket.stock_type.quantity -= 1
ticket.stock_type.save()
# Send the email to the client
# The .send_ticket_email() function is part of the Ticket Model
ticket.send_ticket_email()
elif event_dict['type'] == "payment_intent.payment_failed":
# If the payment has failed:
intent = event_dict['data']['object']
# Get the error message
error_message = intent['last_payment_error']['message']
print("Failed: ", intent['id'], error_message)
# Notify the customer that payment failed
try:
# Find the ticket based on the intent id
ticket = Ticket.objects.get(stripe_payment_intent=intent["id"])
# Change its status to Failed
ticket.status = "Failed"
ticket.save()
# Send Failed email
# the .send_failed_email() function is part of the Ticket model
ticket.send_failed_email()
except Exception as e:
# if the payment intent wasn't found send an email to contact support:
print("Payment intent not found! => {0}".format(e))
elif event_dict['type'] == "payment_intent.canceled":
print("PAYMENT INTENT CANCELED")
elif event_dict['type'] == "payment_intent.amount_capturable_updated":
print("PAYMENT INTENT UPDATED")
else:
print("INTENT = {NONE}")
return HttpResponse(status=200)
(extra) Here is my api/views.py:
class TicketInitialize(generics.GenericAPIView):
queryset = Ticket.objects.all()
serializer_class = TicketSerializer
permission_classes = (permissions.AllowAny,)
def post(self, request, *args, **kwargs):
ticket_price = int(float(request.data["price"])) * 100
print(ticket_price)
stripe.api_key = 'my_stripe_api_key'
intent = stripe.PaymentIntent.create(
amount=ticket_price,
currency='eur',
payment_method_types=['card', 'ideal']
)
request.data["stripe_payment_intent"] = intent["id"]
stock = Stock.objects.get(id=request.data["stock_type"])
event = Event.objects.get(id=request.data["event"])
new_ticket = Ticket.objects.create(
first_name=request.data["first_name"],
last_name=request.data["last_name"],
quantity=request.data["quantity"],
cell_phone=request.data["cell_phone"],
email=request.data["email"],
price=request.data["price"],
service_fee=request.data["service_fee"],
stock_type=stock,
event=event,
stripe_payment_intent=request.data["stripe_payment_intent"]
)
return Response(intent)
my urls.py:
urlpatterns = [
path('payment-stripe/', views.stripe_payment_handling, name="stripe-endpoint"),
]
I don't understand why it keeps giving me the Attribute Error. It won't allow me to handle any webhooks from stripe or complete orders.
Thank you for taking a look, and I'm looking forward to your suggestions and answers!
Change return "Invalid signature", 400 to
return HttpResponse(status=400, content="Invalid signature")
and return "Invalid payload", 400 to
return HttpResponse(status=400, content="Invalid payload")

'ManyToOneRel' object has no attribute 'parent_model' error with django chartit

I have django project with 2 models:
class DeviceModel(models.Model):
name = models.CharField(max_length=255)
def __unicode__(self):
return self.name
class Device(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
device_model = models.ForeignKey(DeviceModel)
serial_number = models.CharField(max_length=255)
def __unicode__(self):
return self.device_model.name + " - " + self.serial_number
There many devices in the database and I want to plot chart "amount of devices" per "device model".
I'm trying to do this task with django chartit.
Code in view.py:
ds = PivotDataPool(
series=[
{'options': {
'source':Device.objects.all(),
'categories':'device_model'
},
'terms':{
u'Amount':Count('device_model'),
}
}
],
)
pvcht = PivotChart(
datasource=ds,
series_options =
[{'options':{
'type': 'column',
'stacking': True
},
'terms':[
u'Amount']
}],
chart_options =
{'title': {
'text': u'Device amount chart'},
'xAxis': {
'title': {
'text': u'Device name'}},
'yAxis': {
'title': {
'text': u'Amount'}}}
)
return render(request, 'charts.html', {'my_chart': pvcht})
This seems to plot result I need, but instead of device names it plots values of ForeignKey (1,2,3,4...) and I need actual device model names.
I thought that solution is to change 'categories' value to:
'categories':'device_model__name'
But this gives me error:
'ManyToOneRel' object has no attribute 'parent_model'
This type of referencing should work accoring to official example http://chartit.shutupandship.com/demo/pivot/pivot-with-legend/
What am I missing here?
C:\Anaconda\lib\site-packages\django\core\handlers\base.py in get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
if response:
break
if response is None:
wrapped_callback = self.make_view_atomic(callback)
try:
response = wrapped_callback(request, *callback_args, **callback_kwargs) ###
except Exception as e:
# If the view raised an exception, run it through exception
# middleware, and if the exception middleware returns a
# response, use that. Otherwise, reraise the exception.
for middleware_method in self._exception_middleware:
response = middleware_method(request, e)
D:\django\predator\predator\views.py in charts
series=[
{'options': {
'source':Device.objects.all(),
'categories':'device_model__name'
},
#'legend_by': 'device_model__device_class'},
'terms':{
u'Amount':Count('device_model'), ###
}
}
],
#pareto_term = 'Amount'
)
C:\Anaconda\lib\site-packages\chartit\chartdata.py in __init__
'terms': {
'asia_avg_temp': Avg('temperature')}}]
# Save user input to a separate dict. Can be used for debugging.
self.user_input = locals()
self.user_input['series'] = copy.deepcopy(series)
self.series = clean_pdps(series) ###
self.top_n_term = (top_n_term if top_n_term
in self.series.keys() else None)
self.top_n = (top_n if (self.top_n_term is not None
and isinstance(top_n, int)) else 0)
self.pareto_term = (pareto_term if pareto_term in
self.series.keys() else None)
C:\Anaconda\lib\site-packages\chartit\validation.py in clean_pdps
def clean_pdps(series):
"""Clean the PivotDataPool series input from the user.
"""
if isinstance(series, list):
series = _convert_pdps_to_dict(series)
clean_pdps(series) ###
elif isinstance(series, dict):
if not series:
raise APIInputError("'series' cannot be empty.")
for td in series.values():
# td is not a dict
if not isinstance(td, dict):
C:\Anaconda\lib\site-packages\chartit\validation.py in clean_pdps
try:
_validate_func(td['func'])
except KeyError:
raise APIInputError("Missing 'func': %s" % td)
# categories
try:
td['categories'], fa_cat = _clean_categories(td['categories'],
td['source']) ###
except KeyError:
raise APIInputError("Missing 'categories': %s" % td)
# legend_by
try:
td['legend_by'], fa_lgby = _clean_legend_by(td['legend_by'],
td['source'])
C:\Anaconda\lib\site-packages\chartit\validation.py in _clean_categories
else:
raise APIInputError("'categories' must be one of the following "
"types: basestring, tuple or list. Got %s of "
"type %s instead."
%(categories, type(categories)))
field_aliases = {}
for c in categories:
field_aliases[c] = _validate_field_lookup_term(source.model, c) ###
return categories, field_aliases
def _clean_legend_by(legend_by, source):
if isinstance(legend_by, basestring):
legend_by = [legend_by]
elif isinstance(legend_by, (tuple, list)):
C:\Anaconda\lib\site-packages\chartit\validation.py in _validate_field_lookup_term
# and m2m is True for many-to-many relations.
# When 'direct' is False, 'field_object' is the corresponding
# RelatedObject for this field (since the field doesn't have
# an instance associated with it).
field_details = model._meta.get_field_by_name(terms[0])
# if the field is direct field
if field_details[2]:
m = field_details[0].related.parent_model ###
else:
m = field_details[0].model
return _validate_field_lookup_term(m, '__'.join(terms[1:]))
def _clean_source(source):
In categories you can only use fields that are included in source queryset.
On the other hand in terms you can use foreignKey or manyTomany connected fields.
Find an example below.
Instead of using
'source':Device.objects.all()
'categories':'device_model'
try to use
'source':DeviceModel.objects.all()
'categories':'name'
and next
'Amount':Count('device__device_model')
I think there is a problem with newer version of django (1.8).
This code is deprecated:
m = field_details[0].related.parent_model
instead of it use
m = field_details[0].getattr(field.related, 'related_model', field.related.model)
You can also find fix to this problem on GitHub.
Hope this helps.

Adding ManytoManyField breaks Django

In my project, I have a model, Project, that when saved creates another model, Access, which contains a manytomanyfield, access_list, where users can add other users onto their project as collaborators. It works - when I create a new project, I can add additional users into it, but if I add a 2nd user, it will no longer serve the page with the error,
Exception Value: get() returned more than one Access -- it returned 2!"
If I switch to an account I've added to the project, then add other users on with that account, they add fine and it does not break the page.
When the page breaks, it also creates an additional instance of the project on my Projects page, even though there's only one instance of the project in the database.
My code:
Models.py:
class Project(models.Model):
created_by = models.ForeignKey(User)
super(Project, self).save()
Access.objects.get_or_create(project=self)
class Access(models.Model):
project = models.ForeignKey(Project)
access_list = models.ManyToManyField(User)
pubdate = models.DateTimeField(default=timezone.now)
Views.py:
#login_required
def access(request, project_id=1):
thisuser = request.user
if Access.objects.filter(Q(access_list=thisuser) | Q(project__created_by=thisuser), project__id=project_id).exists():
accesspermission = Access.objects.filter(Q(access_list=thisuser) | Q(project__created_by=thisuser), project__id=project_id).order_by('-project__project_pubdate')[0]
else:
accesspermission = None
if Entry.objects.filter(project_id=project_id).exists():
anyentries = Entry.objects.filter(project_id=project_id, entry_unique=1).order_by('-entry_pubdate')[0]
else:
anyentries = None
if Entry.objects.filter(project_id=project_id, entry_unique=1).exists():
firstentry = Entry.objects.filter(project_id=project_id, entry_unique=1).order_by('-entry_pubdate')[0]
else:
firstentry = None
if Entry.objects.filter(project_id=project_id).exists():
lastentry = Entry.objects.filter(project_id=project_id).order_by('-entry_pubdate')[0]
lastentrynumber = lastentry.entry_unique
else:
lastentrynumber = None
if request.method == "POST":
form = AddAccessForm(request.POST)
if form.is_valid():
p = form.save(commit=False)
adduserfromform = p.accessupdate
if User.objects.filter(username=adduserfromform).exists():
usertoadd = User.objects.get(username=adduserfromform)
projecttoadd = Access.objects.filter(project__id=project_id).order_by('-project__project_pubdate')[0]
projecttoadd.access_list.add(usertoadd)
else:
usertoadd = None
removeuserfromform = p.accessremove
if User.objects.filter(username=removeuserfromform).exists():
usertoremove = User.objects.get(username=removeuserfromform)
projecttoremove = Access.objects.filter(project__id=project_id).order_by('-project__project_pubdate')[0]
projecttoremove.access_list.remove(usertoremove)
else:
usertoremove = None
form.save()
return HttpResponseRedirect('/projects/get/%s/access' % project_id)
Traceback:
Traceback (most recent call last):
File "/webapps/filmeditdb/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/webapps/filmeditdb/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/webapps/filmeditdb/filmeditdb/docproject/views.py", line 284, in access
def access(request, project_id=1):
File "/webapps/filmeditdb/local/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
return self.get_queryset().get(*args, **kwargs)
File "/webapps/filmeditdb/local/lib/python2.7/site-packages/django/db/models/query.py", line 310, in get
(self.model._meta.object_name, num))
MultipleObjectsReturned: get() returned more than one Access -- it returned 2!
2014-03-25 22:48:26 [17264] [INFO] Handling signal: winch
2014-03-25 22:48:26 [17264] [INFO] SIGWINCH ignored. Not daemonized
Adding a full traceback is generally recommeded, but from the error shown the issue is most likely on this line:
projecttoremove = Access.objects.get(project__id=project_id).order_by('-project__project_pubdate')[0]
Djangos get() always expects to return only 1 object. If there is 0 objects, it throws a DoesNotExist exception if there is more than 0, it throws MultipleObjectsReturned Exception.
Change that line to match the earlier line here:
projecttoadd = Access.objects.filter(project__id=project_id).order_by('-project__project_pubdate')[0]
Failing this, identify the erroneous call. The exception you are getting is quite clear.
At some point in the code, you are calling Access.objects.get() with some parameters and it is finding 2 objects instead of 1. Either fix the parameters, or moer likely switch it to use filter().

Categories