Thanks for the answer,
I modified my Game model according to your suggestion, and also,
simplified whole thing.
I can create new game without having to choose any tags - that's
correct. Now, take a look at
the save_model() method in GamesAdmin - I try to associate newly
created Game with some
tag -- with no luck :/ Looks like django admin application prevents
from actual saving _any_ relationship
inside save_model in save() method of Game model as well - tested).
Any ideas how to force create of m2m relationship after the form got
submited, without using raw SQL query?
Current code:
#in models.py
class Game(models.Model):
id = models.AutoField(primary_key=True, db_column='grg_id')
tags = models.ManyToManyField(Tag, blank=True, null=True,
related_name='g_tags')
class Tag(models.Model):
id = models.AutoField(primary_key=True, db_column='grt_id')
tag = models.CharField(max_length=255, unique=True,
db_column='grt_tag')
tag_url = models.CharField(max_length=255, blank=False,
db_column='grt_tag_url')
tag_keyword = models.CharField(max_length=255, blank=False,
db_column='grt_tag_keyword')
#in admin.py
class GamesAdmin(admin.ModelAdmin):
#NOT using custom form anymore
#form = GamesAdminModelForm
def save_model(self, request, obj, form, change):
super(GamesAdmin, self).save_model(request, obj, form, change)
if not change:
#get a tag
tag = Tags.objects.get(pk=1)
#associate tag with newly created object
obj.tag.add(tag)
admin.site.register(Game)
admin.site.register(Tag)