Skip to content

Instantly share code, notes, and snippets.

@apinsard
Created June 14, 2015 17:47
Show Gist options
  • Save apinsard/261cb6f6427e50360c5c to your computer and use it in GitHub Desktop.
Save apinsard/261cb6f6427e50360c5c to your computer and use it in GitHub Desktop.
tmp.py
class Degree(models.Model):
class Meta:
ordering = ['-popularity']
order_with_respect_to = 'subcategories'
name = models.CharField(unique=True, max_length=20)
description = models.TextField(blank=True)
popularity = models.PositiveIntegerField(default=0)
alias_of = models.ForeignKey('self', related_name='synonyms', blank=True,
null=True, limit_choices_to={'alias_of': None})
subcategories = models.ManyToManyField('core.Subcategory', blank=True,
related_name='degrees')
def __str__(self):
return self.name
>>> from accounts.models import Degree
>>> d = Degree(name="Foobar", popularity=0)
>>> d.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/tony/.virtualenvs/aladompy/lib/python3.3/site-packages/django/db/models/base.py", line 710, in save
force_update=force_update, update_fields=update_fields)
File "/home/tony/.virtualenvs/aladompy/lib/python3.3/site-packages/django/db/models/base.py", line 738, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/tony/.virtualenvs/aladompy/lib/python3.3/site-packages/django/db/models/base.py", line 814, in _save_table
**{field.name: getattr(self, field.attname)}).count()
File "/home/tony/.virtualenvs/aladompy/lib/python3.3/site-packages/django/db/models/fields/related.py", line 1242, in __get__
through=self.field.rel.through,
File "/home/tony/.virtualenvs/aladompy/lib/python3.3/site-packages/django/db/models/fields/related.py", line 874, in __init__
(instance, source_field_name))
ValueError: "<Degree: Foobar>" needs to have a value for field "degree" before this many-to-many relationship can be used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment