ManyToManyField of Django model populated unexpectedly - python

I'm currently working on a site with Django on Python 3. The site allows users to follow projects that they like, and they can follow as many as you want, as long as the thot projects exist. Here are my models of the site.
This is my Team model, which is the model for all the projects you can follow
class Team(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='team')
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=255)
description = models.TextField()
image = models.ImageField(upload_to='images/team_images', blank=True)
roster = models.TextField(blank=True, default='')
current_raise = models.IntegerField(blank=True, default=0)
# current_backers = models.IntegerField(blank=True, default=0)
time_left = models.DateTimeField(blank=True, default=timezone.now)
raising_minimum = models.PositiveIntegerField(blank=False)
raising_maximum = models.PositiveIntegerField(blank=False)
duration = models.DateTimeField(blank=False)
def __str__(self):
return self.title
This is the user profile mode, I have a Many-to-Many field in this model.
class UserProfile(models.Model):
user = models.OneToOneField('accounts.User')
following = models.ManyToManyField(Team, related_name='user_following', blank=True)
def __str__(self):
return self.user.first_name
def save(self, *args, **kwargs):
if self.pk:
for team in self.following:
team.save()
super(UserProfile, self).save(*args, **kwargs)
The problem is, when I check the database, all the teams I've created for testing are in the field following even though the user hasn't requested to follow them, yet.
All the teams I have created showed up under 'following' in admin page.
enter image description here

Options displayed in the m2m multiple choice field of admin site are just options you can choose from. It is expected behavior of Django admin. To choose some options you can just click on the item or if you want to choose multiple click with CTRL pressed.

Related

Multi-table inheritance and two many to many via through model not working in admin inline

I'm trying to create navigation menu from the django admin as per user's requirement.
The Model look like this:
class MenuItem(models.Model):
title = models.CharField(max_length=200, help_text='Title of the item')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
is_published = models.BooleanField(default=False)
def __str__(self):
return self.title
class Page(MenuItem):
"""
To display non-hierarchical pages such as about us, or some page in menu
"""
slug = models.SlugField(max_length=200, unique=True, help_text='End of url')
content = HTMLField(help_text='Contents of the page')
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING)
class Category(MenuItem):
"""
For hierarchical display eg. in navigation menu use Category and Articles.
"""
slug = models.SlugField(max_length=200, unique=True, help_text='End of url')
class Meta:
verbose_name_plural = 'categories'
Page and Category can be different types of menu items, so, I used inheritance. Now I want to bind the MenuItem to a Menu, hence I added two more models below.
class Menu(models.Model):
title = models.CharField(max_length=200, unique=True, help_text='Name of the menu.')
is_published = models.BooleanField(default=False)
item = models.ManyToManyField(MenuItem, through='MenuLevel', through_fields=['menu', 'menu_item'])
def __str__(self):
return self.title
class MenuLevel(models.Model):
menu = models.ForeignKey(Menu, on_delete=models.CASCADE)
menu_item = models.ForeignKey(MenuItem, on_delete=models.CASCADE, related_name='items')
level = models.IntegerField(default=0)
parent = models.ForeignKey(MenuItem, on_delete=models.CASCADE, related_name='parent', null=True, blank=True)
I need the parent key to menu item to traverse through the menu items from parent to children and level to sort the menu in order.
On the admin I have two simple classes:
class MenuLevelInline(admin.TabularInline):
model = MenuLevel
#admin.register(Menu)
class MenuAdmin(admin.ModelAdmin):
inlines = [MenuLevelInline]
Here is the problem:
If I try to save two categories, one as a parent to another, things work fine. However, if I have a category as a parent and Page as a child I get IntegrityError: FOREIGN KEY constraint failed error.
When I look in the database, the menu_item table does contain all the keys for both Categories and pages table.
What did I do wrong?
Everything just worked once I switched to MySQL. It was not working on Sqlite. I don't know why it doesn't work with sqlite maybe it is a bug. But changing the database solved my problem.

Django ModelForm for Multiple Categories of a Product in EAV data model

Hello all I am making auction website like ebay, I have this model design which has many other extra attributes model classes for different categories. But here let's take an example of a PC one which will be used for its sub-categories Desktop and Laptop.
Now the problem, I want to create ModelForm for users. For instance if user selects Desktop as their product to put on auction, how will that modelform code look like? So the Desktop respected fields are given to the user to fill from the extra_pc_attributes class? The problem is that, wouldn't it get tedious to write separate modelform for each category and also in the views.py use those as objects.
Maybe use Jsonfield instead of creating a whole EAV old-fashioned table for extra attributes? But I am new and I don't know how it will work or even if it applies to this situation.
class Categories(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
class auction_product(models.Model):
product_name = models.CharField(max_length=64)
category = models.ForeignKey(Categories, on_delete=models.CASCADE)
date_added = models.DateField(default=timezone.now)
user = models.ManyToManyField(User, through='product_ownership', related_name='product_user')
product_bid = models.ManyToManyField(User, through='bid', related_name='product_bid')
product_comment = models.ManyToManyField(User, through='comment')
album = models.OneToOneField(ImageAlbum, related_name='product_model', on_delete=models.CASCADE)
def __str__(self):
return self.product_name
#Extra PC Attributes
class extra_pc_attributes(auction_product):
processor = models.CharField(max_length=264)
ram = models.FloatField()
brand = models.CharField(max_length=64)
motherboard = models.CharField(max_length=264)
case = models.CharField(max_length=264)
screen_size = models.FloatField()
weight = models.FloatField()

How to allow only one User to see "UpdateView"?

I have a Users and Jobs. If some User creates a Job, then and only then he/she can edit some information of this Job.
So he visits the url .../job/update/<id>. If the Job is created by him (the User is a ForeignKey in Job, then he can modify data. Otherwise he gets 404 error.
In view function, I would probably get current Users id and compare this id to Jobs ForeignKey.
But there are many patterns and shortcuts in class views so I'm curious how to do that this way.
class EditOrderView(UpdateView):
model = Job
fields = ['language_from','language_to','level','short_description','notes',
'text_to_translate','file']
template_name = 'auth/jobs/update-order.html'
class Job(models.Model):
customer = models.ForeignKey(User, related_name='orders', help_text=u"Zákazník")
translator = models.ForeignKey(User, related_name='jobs', null=True, blank=True, help_text=u"Prekladateľ")
price = models.FloatField(null=True, blank=True, help_text=u"Cena")
language_from = models.ForeignKey(Language, related_name='jobs_from', null=True)
language_to = models.ForeignKey(Language, related_name='jobs_to', null=True)
...
It looks like you can override .get_object() method and include your own logic:
from django.shortcuts import get_object_or_404
class EditOrderView(UpdateView):
model = Job
...
def get_object(self, queryset=None):
return get_object_or_404(self.model, pk=self.kwargs["pk"], customer=self.request.user)

How to execute CASCADE on delete?

I have this model in Django, where a person has the same information from the user provided by Django plus a little bit more information. When I create a new person it requires to create a new user also, that's fine. But when I delete a person the user still remains on my database. What am I missing here ? I would like to delete the user too.
class Person(models.Model):
user = OneToOneField(User)
gender = CharField(max_length=1, choices=GenderChoices, blank=True, null=True)
birth_date = DateField(blank=True, null=True)
def __unicode__(self):
return self.user.username
Try to override the delete method on the model (code not tested):
class Person(models.Model):
user = OneToOneField(User)
gender = CharField(max_length=1, choices=GenderChoices, blank=True, null=True)
birth_date = DateField(blank=True, null=True)
def __unicode__(self):
return self.user.username
def delete():
theuser = User.objects.get(id=user)
theuser.delete()
I have found some relevant documentation about CASCADE usage in Django here.

What is the proper way to structure models.py?

I'm trying to build the right models for my Django app. I'm trying to build something that will allow a user to save a URL into one (or more) playlist(s) that is tied to that user. Before I implement this, I want to make sure that this is the best way to structure my models.py.
class UserProfile(models.Model):
user = models.ForeignKey(User, primary_key=True) #what is the difference between ForeignKey and OneToOne? Which one should I use?
Playlist = models.CharField('Playlist', max_length = 2000) #1 user should be able to have multiple playlists and the default playlist should be "Favorites"
def __unicode__(self):
return self.User
class Videos(models.Model):
Video_url = models.URLField('Link to video', max_length = 200, null=True, blank=True)
Playlist = models.ManyToManyField(Playlist) #this should connect to the playlists a user has. A user should be able to save any video to any plalist, so perhaps this should be ManyToMany?
def __unicode__(self):
return self.Video_url
Woah. Firstly the question is probably too "localised" for SO. Anyway. I'd do it like this:
class PlayList(models.Model):
playlist = models.CharField(max_length=2000)
class UserProfile(models.Model):
# do you want each `User` to only have one `UserProfile`? If so then OneToOne
# primary keys are automatically formed by django
# how django handles profiles: https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
user = models.ForeignKey(User)
def __unicode__(self):
return self.User
class UserPlayList(models.Model):
# don't capitalise attributes, if you haven't seen PEP8 before, do now: http://www.python.org/dev/peps/pep-0008/
profile = models.ForeignKey(User)
playlist = models.ForeignKey(PlayList)
class Video(models.Model):
video_url = models.URLField(max_length=200, null=True, blank=True, help_text="Link to video")
def __unicode__(self):
return self.video_url
class VideoPlayList(models.Model):
video = models.ForeignKey(Video)
play_list = models.ForeignKey(UserPlayList)

Categories