Fix Python – How to create an object for a Django model with a many to many field?

My model:
class Sample(models.Model):
users = models.ManyToManyField(User)

I want to save both user1 and user2 in that model:
user1 = User.objects.get(pk=1)
user2 = User.objects.get(pk=2)
sample_object = Sample(users=user1, users=user2)

I know that’s wrong, but I’m sure you get what I want to do. How would you do it ?

Fix Python – Django self-referential foreign key

I’m kind of new to webapps and database stuff in general so this might be a dumb question. I want to make a model (“CategoryModel”) with a field that points to the primary id of another instance of the model (its parent).
class CategoryModel(models.Model):
parent = models.ForeignKey(CategoryModel)

How do I do this? Thanks!

Fix Python – Convert Django Model object to dict with all of the fields intact

How does one convert a django Model object to a dict with all of its fields? All ideally includes foreign keys and fields with editable=False.
Let me elaborate. Let’s say I have a django model like the following:
from django.db import models

class OtherModel(models.Model): pass

class SomeModel(models.Model):
normal_value = models.IntegerFi….

Fix Python – What’s the difference between select_related and prefetch_related in Django ORM?

In Django doc,

select_related() “follows” foreign-key relationships, selecting additional related-object data when it executes its query.
prefetch_related() does a separate lookup for each relationship, and does the “joining” in Python.

What does it mean by “doing the joining in python”? Can someone illustrate with an example?
My understanding i….