I would like to start my ids on a django model from 1000. I've found this response on Stackoverflow but I am missing something in my implementation because it is not working.
This is my code in apps.py
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.db import IntegrityError
from invoice.models import Invoice
def my_callback(sender, **kwargs):
if sender.name =="invoice":
try:
Invoice.objects.create(id=999)
Invoice.objects.delete()
except IntegrityError:
pass
class InvoiceConfig(AppConfig):
name = 'invoice'
def ready(self):
post_migrate.connect(my_callback, sender=self)
I've then ensure migrate takes place but the model continues to increment from low numbers. What am I missing?
Related
I want to initialize the database table with some predefined instances.
# apps.py
from django.apps import AppConfig
from django.db.models.signals import post_migrate
def initialize(sender, **kwargs):
from .models import Address
Address.objects.create(
# address fields
)
print('Created')
class BackendConfig(AppConfig):
name = 'backend'
def ready(self):
print('Ready')
post_migrate.connect(initialize, sender=self)
However nothing was created and nothing was printed after migration like the signal not triggered at all.
Sorry I am dumbass.
Need to mention default_app_config = 'backend.apps.BackendConfig' in __init__.py
I am using django-simple-history to save history of data. I want to save an extra field value to each history model before it is saved. I found the reference code in documentation mentioned above but cant use it. Please help.
from django.dispatch import receiver
from simple_history.signals import (
pre_create_historical_record,
post_create_historical_record
)
#receiver(pre_create_historical_record)
def pre_create_historical_record_callback(sender, **kwargs):
print("Sent before saving historical record")
#receiver(post_create_historical_record)
def post_create_historical_record_callback(sender, **kwargs):
print("Sent after saving historical record")
apps.py file
from django.apps import AppConfig
class LogAppConfig(AppConfig):
name = 'log_app'
def ready(self):
import log_app.signals
signals.py file
from simple_history.signals import (pre_create_historical_record, post_create_historical_record)
#receiver(pre_create_historical_record)
def pre_create_historical_record_callback(sender, **kwargs):
print("signal is running")
history_instance = kwargs['history_instance']
I made this before with django like this:
signals/handlers.py
from django.dispatch import receiver
from django.conf import settings
from django.contrib.auth.models import Group
from users.models import *
#receiver(post_save, sender=settings.AUTH_USER_MODEL)
def save_profile(sender, instance, created, **kwargs):
if created:
g1 = Group.objects.get(name='Editors')
instance.groups.add(g1)
apps.py
from django.apps import AppConfig
class RegistrationConfig(AppConfig):
name = 'registration'
def ready(self):
import registration.signals.handlers
but I don't know how to make it with wagtail !
thanks.
Instead of using Django signals, Wagtail has hooks that simplify this for you. You can also send password reset email etc... after creating user using the same technique.
Just create a wagtail_hooks.py in your app:
from django.contrib.auth.models import Group
from wagtail.core import hooks
#hooks.register('after_create_user')
def add_user_to_group(request, user):
if user:
group, created = Group.objects.get_or_create(name='Group Name')
user.groups.add(group)
Docs: https://docs.wagtail.io/en/latest/reference/hooks.html?highlight=after_create_user#id40
I read a part of the doc, and some articles, but my code is not working.
OBS: i'm using custom User created with AbstractUser, but i not add extra fields
Look this example
profile.signals.py:
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth import get_user_model
from .models import Profile
User = get_user_model()
#receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
now, look in the user creation:
>>> from accounts.models import User
>>> me = User.objects.create(username='myusr', email='me#email.com', password='me123456')
>>> me
<User: myusr>
>>> me.save()
>>> me.profile
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/mnt/sda4/Development/coding/Projects/codesv3/env/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 415, in__get__
self.related.get_accessor_name()
accounts.models.User.profile.RelatedObjectDoesNotExist: User has no profile.
i dont know what's wrong. Also because I have not used it before and i not know about SQL triggers
In your signals.py file, save profile after it's created, like below:
#receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
Make sure, you import signals in your related apps.py file like below example:
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals
Then also make sure you have made the below change:
you must add app's config to either of two files.
In your settings.py file's INSTALLED_APPS, like mentioned in this link: Django create profile for user signal.
OR,
In your related app's __init__.py file, like this (in this example, the related app is users): default_app_config = 'users.apps.UsersConfig'
First, this command create user and you do not need to save it after creating:
me = User.objects.create(username='myusr', email='me#email.com', password='me123456')
Second, where your signals places? If you put it in models everything should works.
Also you can place where you want but you need to import it in your apps like here:
class ProfileConfig(BaseConfig):
name = ...
def ready():
import profiles.signals # where your signals place
I'm trying to assign a group to every new user registered into the system. I've already read something about it in another questions but I don't really know where to add the necessary code to make it work.
I'm using Django 2.1.3 and I'm logging users using allauth (social login, but it shouldn't make any difference as a new instance in the User table is created)
You can use a #post_save signal for example that, each time a User is created, adds the given group to the groups of the User. Typically signals reside in a file named handlers.py in the signals directory of an app, so you probably should create or modify the files listed in boldface:
app/
signals/
__init__.py
handlers.py
__init__.py
apps.py
...
# app/signals/handlers.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from django.contrib.auth.models import Group
#receiver(post_save, sender=settings.AUTH_USER_MODEL)
def save_profile(sender, instance, created, **kwargs):
if created:
g1 = Group.objects.get(name='group_name')
instance.groups.add(g1)
where group_name is the name of the group you want to add.
You should then import the handlers.py module in your MyAppConfig (create one if you do not have constructed such config yet):
# app/apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'app'
verbose_name = "My app"
def ready(self):
import app.signals.handlers
and register the MyAppConfig in the __init__.py of the app:
# app/__init__.py
default_app_config = 'app.apps.MyAppConfig'
If this should happen for any new User instance, you can connect a handler to the post_save signal:
from django.db.models.signals import post_save
from django.dispatch import receiver
#receiver(post_save, sender=User)
def handle_new_job(sender, **kwargs):
if kwargs.get('created', False):
user = kwargs.get('instance')
g = Group.objects.get(name='whatever')
user.groups.add(g)
Include this code in your app and make sure it is imported as stated e.g. here.