Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7799673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:23:19+00:00 2026-06-02T00:23:19+00:00

I’d like to use a string as the sender for a custom Django signal

  • 0

I’d like to use a string as the sender for a custom Django signal but have run into some issues related to how Django’s unicode encoding of a model’s strings. Here’s a hopefully working short example illustrating the problem:

from django.dispatch import Signal
from django.db import models

example_signal = Signal(providing_args=["data"])

class Example(models.Model):
    example_field = models.CharField(max_length=32)
    def send_signal(self):
        example_signal.send(sender=self.example_field, data=self) # (arbitrarily using self as the signal payload)

def example_handler(sender, data=None):
    print "received data %s" % data

example_signal.connect(example_handler, sender=u'boogat')


... (after entering the django shell and importing the models)
>>> t = Example.objects.create(example_field='boogat')
>>> t.send_signal()
>>>  

I’ve skimmed through Django’s dispatcher.py code – in this case, the signals dispatcher appears to use the Python builtin id function to generate a unique id for whatever object is used as the sender. However, I can’t seem to figure out what I need to do to programmatically generate a string that, when sent to id(), is equivalent to Django’s unicode model string. I’ve tried str, repr, encode('UTF-8'), django.util.encodings, all to no avail.

At this point I’ve already implemented a workaround but I’d still like to understand what is going on… any input would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:23:21+00:00Added an answer on June 2, 2026 at 12:23 am

    I ran into a similar problem with signals and found what I think is a better solution.

    I definitely prefer using the sender argument of connect to checks inside the handler if possible.

    The problem as you say is object id(): two strings (not even just unicode strings!) with the same content may have different object ids. The solution is the intern() built-in which enters the given string into python’s internal identifier table (this is very similar to ruby’s Symbol).

    If you use sender=intern(sender_string) on both send() and connect() things should work as expected.

    Two important caveats:

    1. intern() only works on str, not unicode – you will have to deal with encoding back to str, and you have to perform the same encoding for both send() and connect().
    2. Python’s internal record of your interned string can be discarded by the garbage collector when it discards the reference to your interned string, so you have to make sure you keep it around.

    A good way to deal with both problems is that you are probably only interested in signals from a few pre-defined strings, so just stick those in a configuration constant, already interned.

    For example:

    user_did_something = Signal(providing_args=["data"])
    
    class User(models.Model):
        identifier = models.CharField(max_length=32)
        def send_signal(self):
            user_did_something.send(sender=intern(self.example_field.encode('utf8')), data=self) 
    
    
    ADMIN_USER = intern('admin')
    BIG_ADMIN_USER = intern(u'größer admin'.encode('utf8'))
    
    
    user_did_something.connect(admin_behavior, sender=ADMIN_USER)
    user_did_something.connect(big_admin_behavior, sender=BIG_ADMIN_USER)
    

    BIG_ADMIN_USER will print out to gibberish if not decoded back to a unicode string, but I suspect most such identifiers will be ascii.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.