build a follower system which is not between enduser - python

Usecase
The app that I am developing is about linking startup, job seekers and investors. Sorry I could not give
the name still though I have developed already around 40% :) .There will be three entities. One
is Startup account, Enduser account and Investor account. I am thinking of adding follow/unfollow feature where
Enduser can follow Startup and vice-versa
Startup can follow Investor and vice-versa
Enduser cannot follow other enduser and enduser cannot follow investor
For this how should I model my application
Here is the model for the entities I talked about
Enduser Model
class UserSetting(models.Model):
user = models.OneToOneField(User)
job_interest = models.ManyToManyField(Category, related_name='user')
is_email_notification = models.BooleanField(
default=True, help_text="Email me job recommendations based on my interests and preferences")
class Meta:
verbose_name = 'User Setting'
verbose_name_plural = 'User Settings'
def __str__(self):
return self.user.username
Startup Model
class Startup(models.Model):
name = models.CharField(max_length=200, unique=True,
blank=False, null=False)
slug = models.SlugField(unique=True)
description = models.CharField(max_length=400)
Investor Model
class Investor(models.Model):
investor = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=200, blank=False,
null=False, help_text='Full Name')
slug = models.SlugField(max_length=200, unique=True)
contact_email = models.EmailField(null=True, blank=True)
# for now I have done like this
followers = models.ManyToManyField(
User, related_name='followers', blank=True)
For now, you can see I have included the followers field in the Investor but I need the follower system in the startup as well so that enduser can follow startup and vice-versa. Also in following investor only startup can follow but enduser should not.
What is the best possible solution for this?

It sounds like you are looking to build junction objects. You want to register Users against those objects. So I would probably redesign your data model a little. Here are my thoughts:
Investors are a group of users under a name (Pasadena Angels, XYZ Venture Partners, etc)
Startup is a company with a group of users
End-User is a singular User
But django authenticated Users can be "Investors", "Startup Employees", or "End Users". AND it is more than likely that Angel Investors are Executive Employees at a Startup. And it's possible, actually it sounds desireable that over time an EndUser becomes a Startup Employee.
Therefore I'd do something like this:
class EndUser(models.Model):
user = models.OneToOneField(User)
...
class Startup(models.Model):
name = models.CharField(max_length=100)
...
class StartupEmployee(models.Model):
user = models.ForeignKey(User)
startup = models.ForeignKey(Startup)
...
class InvestmentFirm(models.Model):
name = models.CharField(max_length=100)
...
class Investor(models.Model):
user = models.ForeignKey(User)
firm = models.ForeignKey(InvestmentFirm)
...
class Follower(models.Model):
user = models.ForeignKey(User)
startup = models.ForeignKey(Startup, blank=True, null=True)
firm = models.ForeignKey(InvestmentFirm, blank=True, null=True)
...
My thought process is that the data model does not need to control the logic of who can follow who. My thought process is that the application will have separate views which can control who can see what. In other words the data model shouldn't prevent end-users from following other end-users. That should be part of the UI/UX.
Thats my quick hack at a recommendation.

Related

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()

Django Multiple-User Model

I need advice on a multiple user type.
Scenario:
A user can be an organization, where in this organization they can place adverts on the website. The owner of this organization(user) can edit/delete users and adverts of his own organization(group). In this organization user type there are users that also can log in and they can only see the adverts placed by them, but the owner of this group must see all adverts of his own and of his users. Think like an estate listing where an organization has multiple locations/users that can place adverts, and has to be managed by a admin user of this organization.
What type or model is the best/cleanest for implementing this in a good way? Do I need the Django's User and Group model?
One solution would be to have the "User Profiles" structure.
So you use the standard Django User Model and you attach to it several OneToOne relationships depending on the number of profile types you'll have. This has the advantage of allowing users to cover more than one role at the same time.
For example:
from django.contrib.auth.models import User
class Organization(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="organization")
name = models.CharField(max_length=50, blank=True, null=True)
class Supervisor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="supervisor")
name = models.CharField(max_length=50, blank=True, null=True)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name="supervisors")
class CustomUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="custom_user")
name = models.CharField(max_length=50, blank=True, null=True)
supervisor = models.ForeignKey(Supervisor, on_delete=models.CASCADE, related_name="custom_users", blank=True, null=True)
And then when you go and create the models for the ads to be displayed on the website you can use the built-in PermissionRequiredMixin.
In order to do that you have to start by adding "permissions" in the ad model Meta class:
class Ad(models.Model):
# fields
class Meta:
permissions = [
('can_edit_ads', 'org_representative')
]
Then on your view you have to extend the PermissionRequiredMixin, example:
class EditAd(UpdateView, PermissionRequiredMixin):
model = Ad
template_name = "ad123.html"
permission_required = "ad.can_edit_ads"
A quick way to test it is by going in the user table on the admin panel, open a user detail page where you can see all the permissions, and there alongside the others you'll find your custom one as well.
From there you can easily assign the new permission to the specific user.

how to use temporary database for every user in django

so i am learning Django and making a todo application but i am stuck with a problem. I am storing every task on sqlite3 database(default by django) with models but when anyone store any task it is also visible to other person also how can i fix that?
if you have any alternative solution for that you can also tell that like how to use cookies or anything
models.py:-
from django.db import models
# Create your models here.
class Task(models.Model):
title = models.CharField(max_length=200)
completed = models.BooleanField(default=False, blank=True, null=True)
def __str__(self):
return self.title
Store who the task belongs to,
class Task(models.Model):
title = models.CharField(max_length=200)
completed = models.BooleanField(default=False, blank=True, null=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
You will have to store the user when creating/updating the Task
For instance,
task = Task.objects.create(title=title, user=user)
Now when you need to show the tasks, show only relavant tasks
tasks = Task.objects.filter(user=request.user)

Building a contribution functionality in django (i do not necessarily need the code just an idea)

NOTE I am not necessarily asking for code to build this, just ideas on how to do this. Links and blog posts for pointers are welcome.
I am building a rest api.
I have a model
class Showcase(models.Model):
title = models.CharField(max_length=50)
description = models.TextField(null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name="Showcases")
created_on = models.DateTimeField(auto_now_add=True)
updated_on = models.DateTimeField(auto_now=True)
slug = models.SlugField(max_length=255, unique=True)
def __str__(self):
return self.title
I am trying to build a functionality where the user that created a showcase can add users that contributed to the project which is the showcase. I was thinking of making this its own model like this:
class Collaborator(models.Model):
post = models.ForeignKey(Showcase, on_delete=models.CASCADE, related_name="collaborated_showcases")
owner = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name="showcase_owner")
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name="collaborators")
skill = models.ForeignKey(Skill, on_delete=models.CASCADE, null=True, related_name="creative_type")
role = models.TextField(null=True)
added_on = models.DateTimeField(null=True)
def __str__(self):
return f"{self.user.name} collaborated on {self.post.name}"
The user would have to search for users and then add them as a contributor to the showcase, which is where my brain scrambles a bit.
The other important thing is that I want to be able to randomly go to a user and get ALL the showcases he has contributed to.
As I see it, this structure works fine for your use case, though:
models should always be in singular case (Collaborator)
related_names should be lower case (related_name="showcases")
and I prefer to explicitly spell out all related_names, so I'd add
Collaborator.post related name collaborated_showcases
Collaborator.user related name collaborators
Showcase.user related_name owned_showcases
Then,
To find an user's owned showcases, Showcase.objects.filter(user=user)
To find an user's collaborated showcases, Showcase.objects.filter(collaborators=user) (I think)
I'd suggest having a Collaborator object for the owner user as well, so you can show their role more easily as well as make these queries simpler.

Django relations between Models

What I currently have in my models is this:
class Project(models.Model):
project_name = models.CharField(max_length=255, unique=True, blank=False)
def __str__(self):
return str(self.project_name)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
role = models.CharField(choices=ROLE_CHOICES, max_length=255, default='Agent')
Now my question is: Users should be able to have multiple Projects - so I obviously can't use a OneToOne-Field in the Profile-Model.
Later I want to use it for example to just show a user news which are only related to the projects he participates in.
What would be the best strategy to make this possible? Any input is highly appreciated.
Use ManyToMany on project.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
role = models.CharField(choices=ROLE_CHOICES, max_length=255, default='Agent')
project = models.ManyToManyField(Project)
This way one profile can have as many project as he/she wants
On your view you can use this field to filter based on project

Categories