Django extends existing apps - python

I installed app https://github.com/zerok/django-flatblocks, next I want to extend this app model https://github.com/zerok/django-flatblocks/blob/master/flatblocks/models.py. New models.py looks
class MyFlatBlock(FlatBlock):
my_field = models.CharField()
How can I do this, whitout coping all app (django-flatblocks) to my project and rewrite this model?

Create another model
class MyFlatBlock(models.Model):
flat_block = models.OneToOneField(FlatBlock, related_name='extra')
my_field = models.CharField()
Then you will be able to use flatblock.extra.my_field (catching AttributeError if necessary).

Related

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):
...

Multiple Django Admin Arguments with Extensions

Is there a way to use multiple Django extensions in the admin.site.register() inside admin.py? I'm using "simple-history" and "import-export" extensions, but I can only have one of them in the admin.site.register().
Example: I have a model named, "Cars", that is using the "simple-history" extension so I need admin.site.register(Cars, SimpleHistoryAdmin), as their documentation says it should. I want to use the import/export extension as well to the same "Cars" model, but the admin.site.register() doesn't take multiple arguments for me to add it.
models.py
class Cars(models.Model):
Year = models.CharField(max_length=30)
Make = models.CharField(max_length=30)
Model = models.CharField(max_length=30)
history = HistoricalRecords()
class Meta:
verbose_name_plural = "Car Table"
def __str__(self):
return self.Make
admin.py
class CarResource(resources.ModelResource):
class Meta:
model = Cars
fields = ('id','Year', 'Make', 'Model',)
class CarAdmin(ImportExportModelAdmin):
resource_class = CarResource
pass
#I want to use the import/export extension (code above), along with simple-history
admin.site.register(Cars, CarAdmin)
admin.site.register(Cars, SimpleHistoryAdmin)
I've tried using a proxy and inlines, but the proxy makes a new model which I don't want and when using inlines I get an error saying that it requires a foreign key, but I'm not trying to get the model objects from a different model. Naming them the same model doesn't work because the model is already registered. Any help is much appreciated!
In python, class can have more than one parent. Just inherit from 2 parents at once. But both ImportExportModelAdmin and SimpleHistoryAdmin are inheriting from ModelAdmin, that's not good. There is also ImportExportMixin, we can use it instead of ImportExportModelAdmin, so there will be only one reference to ModelAdmin.
class CarResource(resources.ModelResource):
class Meta:
model = Cars
fields = ('id','Year', 'Make', 'Model',)
class CarAdmin(ImportExportMixin, SimpleHistoryAdmin):
resource_class = CarResource
pass
#I want to use the import/export extension (code above), along with simple-history
admin.site.register(Cars, CarAdmin)

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

Recursive Inline with Foreign Fields Django Admin

I am trying to open django admin for the following models..
class FirstModel(models.Model):
name = models.CharField(max_length=100)
class SecondModel(models.Model):
name = models.CharField(max_length=100)
firstModel = models.ForeignKey(FirstModel, related_name='secondList')
class ThirdModel(models.Model):
name = models.CharField(max_length=100)
secondModel = models.ForeignKey(SecondModel, related_name='thirdList')
I am trying to create an admin.py for the following models as follows..
class ThirdModelInline(admin.TabularInline):
model = ThirdModel
extra = 1
class SecondModelInline(admin.StackedInline):
model = SecondModel
inlines = [ThirdModelInline]
class FirstModelAdmin(admin.ModelAdmin):
inlines = [SecondModelInline]
admin.site.register(FirstModel, FirstModelAdmin)
I want to be able to edit the SecondModel and ThirdModel as a recursive relation inside FirstModel. But this is not working. I tried to follow this link : [Model with recursive self relation in Django's admin
[1]: Model with recursive self relation in Django's admin. Any help would be appreciated. Thanks!!
Found a very good library after some websearch. Might help someone else..
https://github.com/s-block/django-nested-inline
django-nested-inline isn't (yet?) supported on latest django releases.
But you could consider using django-nested-admin that is almost the same.

Categories

Resources