Two models pointing to one database table in Django 2.1 - python

I'm working on a Django project made by a former employee of the company (so I'm refactoring a whole project made by somebody else that didn't follow Django Best Practices), that have two models on different apps using the same tables on database. The City and State tables are used in both apps.
I want to know which is the best way to apply DRY concepts and use only one model for the two apps access these tables.
The two apps are on the project folder and each one has his own models.py with the following code for city/state:
from django.db import models
from django.contrib.auth.models import User,Group
from django.db.models.signals import post_save
from django.dispatch import receiver
class state(models.Model):
class Meta:
db_table = '"db_property"."state"'
created_at = models.DateTimeField(db_column='created_at')
updated_at = models.DateTimeField(db_column='updated_at')
name = models.CharField(db_column='name',max_length=50)
class city(models.Model):
class Meta:
db_table = '"db_property"."city"'
created_at = models.DateTimeField(db_column='created_at')
updated_at = models.DateTimeField(db_column='updated_at')
name = models.CharField(db_column='name',max_length=50)
state = models.ForeignKey(state,on_delete=models.CASCADE)

Am I missing something?
Put city and state in one or other app, or even in their own citystate app, and import them from the place they are defined. In an app called foo:
from citystate.models import city, state
In passing, Django models are classes, and as such one would normally start them with a capital letter: City and State. Honoring conventions like this matter: you may not be confused (yet) but you will confuse the heck out of anybody else reading this code, who will think that these things being imported are functions not classes!
An app is not required to have any views, urls, etc. It can be just a place to put common models and their migrations, and maybe some admin classes.

Related

How to connect models from different apps in Django?

I have a few apps within my Django project. There are two apps that I am currently working with "Application" and "User" and I have two questions related to models:
Question 1:
I want to design it in such a way so that external users submit their contact form on Application/templates/Application/Apply.html and the info would get added to the database. Internal users would be able to add external users as well but from a different template: User/templates/User/AddNewContact.html
I am able to add a new contact from an internal user's perspective:
User/models.py
class Contact(models.Model):
ContactName = models.CharField(max_length = 250, default='')
ContactResidence = models.CharField(max_length = 250, default='')
Tel = models.CharField(max_length = 250, default='')
def get_absolute_url(self):
return reverse('User:ContactDetails', kwargs={'pk': self.pk}
)
def __str__(self):
return self.ContactName
class Locations(models.Model):
contact = models.ForeignKey(Contact, on_delete=models.CASCADE)
Country = models.CharField(max_length=250, default='')
def __str__(self):
return self.Country
I was going to just copy this model and paste it into Application/models.py but there are two problems:
1) I don't want external users to be directed to URL: User:ContactDetails and technically, it is not going to work out because I will build the authentication later on.
2) I feel that by copying and pasting I am breaking the 'don't repeat yourself" rule.
Should I connect two models using the foreign keys? What are the best practices in this case?
Question 2
Am I working with one-to-many relationship according to the model provided? I want to have one contact with his personal info (tel/email/address) and a number of branch locations across the world associated with that contact.
To be used a relationship one to many, you can be doing as after:
On models of father app (father table):
class Department(models.Model):
dept_id = models.AutoField(primary_key=True)
On models of child app (child table):
from appname.models import Department
class Office(models.Model):
office_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
dept = models.ForeignKey(Department, on_delete=models.CASCADE)
It helped me.
Question 1: Well, you don't need to copy paste the model. You can use models from other django apps anytime, just need to import it. Basically what you should do is, instead of linking the url directly to the template in the Applications app, you should connect it to a view. In the view file you can import the models from User.models import *, and use them normally.
Question 2: As far as I understand the question your structure provides what you want: one contact (with personal info) associated with several countries. Except that you should replace Agent by Contact in contact = models.ForeignKey(Agent, on_delete=models.CASCADE)
Question 1: Note that the 'get_absolute_url' method is only called if you don't provide a success url in your view. If you are using a CreateView or FormView you can specify the success url by overriding the get_success_url method, for example:
class ContactCreateView(CreateView):
model = Contact
fields = ['ContactName', 'ContactResidence', 'Tel']
def get_success_url(self):
if not self.request.user.internal: # e.g. internal is a User bool field
return HttpResponseRedirect('some/external/url/')
return super().get_success_url() # call get_absolute_url model method.
The DRY principle is respected.
Question 2: Yes, the question you need to ask yourself is 'does a model instance (In this case Contact) have many instances of another model (Location)?' If the answer is yes, then the M2M field should go into your Contact model. See the django docs explaining the pizza/toppings example.
The apps should be in the same project and you can import one model as:
import appName.models or
from appName.models import ClassName
In app2 models you can use foreignKey or manyTomany after importing the class:
from appsName.models import ClassName
class Person(models.Model):
con = ForeignKey(ClassName)

Django ListView not showing recently created items

I have an app with lots of class based views. In some of my ListViews, the items recently created are not being displayed. My models are like:
# models.py
class Parent(models.Model):
name = models.CharField(max_length=10)
class Child(models.Model):
parent = models.ForeignKey(Parent)
name = models.CharField(max_length=10)
And
# views.py
class ChildListView(ListView):
model = Child
template = 'child.html'
The thing is that, whenever I create an object from a CreateView, it doesn't appear in my list view. I thought it was because of sqlite in my developing server, but it happens also in MySQL. If I restart the service, it works normally. Any idea?

Correct way to extend AbstractUser in Django?

I'm trying to integrate two django apps where each had their individual auths working. To do that, I'm trying to subclass AbstractUser instead of User. I'm following the PyBB docs and Django#substituting_custom_model. I've removed all migration files in all my apps apart from their individual init.py (including the migrations from the PyBB library sitting in my site-packages). I've also changed the Mysql database to a blank one to start afresh and I'm trying to subclass AbstractUser as shown below.
My Models.py:
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
from django.db import models
class Student_User(models.Model):
"""
Table to store accounts
"""
su_student = models.OneToOneField(AbstractUser)
USERNAME_FIELD = 'su_student'
su_type = models.PositiveSmallIntegerField(db_column='su_type', default=0)
su_access = models.TextField(db_column='su_access', default='')
su_packs = models.TextField(db_column='su_packs', default='')
REQUIRED_FIELDS = []
def __unicode__(self):
return str(self.su_student)
My settings.py:
AUTH_USER_MODEL = "app.Student_User"
PYBB_PROFILE_RELATED_NAME = 'pybb_profile'
When running makemigrations for my primary app, I get this error:
app.Student_User.su_student: (fields.E300) Field defines a relation with model 'AbstractUser', which is either not installed, or is abstract.
How do I achieve what I am trying to do here?
PS: The app was working fine with onetoone with User without username_field or required_field.
PPS: I just checked the AbstractUser model in my contrib.auth.models and it has class Meta: abstract = True. Ok so its abstract, still, how do I resolve this? I just need one login, currently, two parts of my site, although connected through urls, ask for seperate logins and dont detect each others logins. What do I need to do for this?
You can't have a one-to-one relationship with an abstract model; by definition, an abstract model is never actually instantiated.
AbstractUser is supposed to be inherited. Your structure should be:
class Student_User(AbstractUser):
...

where to put relation models in django

First the simplified scenario:
from django.db import models
class Product(models.Model):
name = models.TextField()
description = models.TextField()
class Merchant(models.Model):
name = models.TextField()
class MerchantProductMapping(models.Model):
merchant = models.ForeignKey(Merchant)
product = models.ForeignKey(Product)
price = models.IntegerField()
inventory_limit = models.IntegerField()
I have another model for the relation (MerchantProductMapping) because the relation has attributes of its own. Now the requirements of the Merchant and the Product model have grown to a point where they demand separate apps of their own. The merchant app's models.py is where the Merchant model will live and the product app's models.py is where the Product model will live.
What I need help with is the relation model MerchantProductMapping. It is needed by both apps, where should I put it ? I've been reading up on mixins and wondering if they could help me somehow.
EDIT: I should add that the app was rendered server side earlier. Now it will be done using angular client - REST api approach. And django rest framework will be used on top of django.
Create "common" app for such purposes ... you can put there decorators, templatetags, base forms, base models, login|logout redirect views and urls, ajax views, base filters, base tables ... etc
Note: create "apps" python package dir (dir with __init__.py inside it) and (refactor) move all your apps there.
EDIT:
Another way - create "models" python package dir and split your models.py to logically separated files inside package

Django M2M Through extra fields with multiple models

I'm trying to figure out the best way to set up the following django model (genericised for security reasons).
ThingA:
User(M2M through "UserRelation")
ThingB:
User(M2M through "UserRelation")
ThingC:
User(M2M through "UserRelation")
User:
Login_name
UserRelation:
User (foreginkey)
Thing (foreignkey) #is this generic to any of the above "things"
Privilege
I understand using "through" between two distinct models, but I'm not sure how to apply this to multiple models. Would I define a foreignkey for each of the "Thing" models in my UserRelation Model?
It looks like you are trying to setup a generic many-to-many relationship. There is a dedicated django app that you can be use for this purpose: django-gm2m
Here is how to use it in your generic case:
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from gm2m import GM2MField
class ThingA(models.Model):
pass
class ThingB(models.Model):
pass
class ThingC(models.Model):
pass
class User(models.Model):
login_name = models.CharField(max_length=255)
things = GM2MField(through='UserRelation')
class UserRelation(models.Model):
user = models.ForeignKey(User)
thing = GenericForeignKey(ct_field='thing_ct', fk_field='thing_fk')
thing_ct = models.ForeignKey(ContentType)
thing_fk = models.CharField(max_length=255)
privilege = models.CharField(max_length=1)
You can now access all the things for a given user and all the User instances for a given 'thing', as well as the privilege attribute for each UserRelation instance.
This will additionally provide you with a handful of benefits (reverse relations, prefetching, etc.) you may need. A GM2MField basically behaves exactly like a django ManyToManyField.
Disclaimer: I am the author of django-gm2m

Categories

Resources