30
Aug 13

Using South to change a Django model’s parent class

I recently wanted to introduce a new parent model for some of my Django models. (The model is called “PayableModel” and is used for any model for which you might pay money.)

Like any reasonable person, I use South to manage my models. This is a Good Thing.

However, much like changing AUTH_USER_MODEL, there is not much support for how to go through this process and keep your existing data. So, in the hopes it’s useful, here’s what I did.

Note: this is a one-way migration. Your model’s ‘id’ field is going to be dropped, and it’s pretty hard to get that back (although now I have some ideas). In any case…

  1. Create your new parent model and run schemamigration as normal (python manage.py schemamigration your_app_name –auto)
  2. In the to-be child model(s), create a new nullable IntegerField called parentmodel_ptr. For example:
    paymentmodel_ptr = models.IntegerField(null=True)
  3. Run schemamigration again. If you’re into that sort of thing, add a “depends_on” clause to the migration to make it dependent on the migration from step #1.
  4. Create a datamigration. (Fancy!):
    python manage.py datamigration your_app_name \
      populate_me_pointers
  5. Edit the datamigration:
    def forwards(self, orm):
      for child in orm.ChildModel.objects.all():
        # in which we create the parent:
        parent = orm['parent.ParentModel']()
        parent.save()
        child.parentmodel_ptr = parent.pk
        child.save()

    This snazzily creates the parents and links it to the secret Django magic _ptr field that will tell Django where to find the parent. At this point, the field is nothing special.

  6. Edit your model to (1) remove the parentmodel_ptr and at the same time add the new parent class. Then run another schemamigration.

The brilliance of this model is I have idea how South converts the IntegerField to the foreign key. It will see the field as changing, rather than being deleted and re-added, because you made the change all at once.

I hope this helps!

If there was one post that made me want to switch to a Markdown-based blog…


15
Aug 13

The best way to protect your identity is to lie

“Assume rational actors.”

Part of Nixon’s strategy for the Cold War was to act crazy. International politics, along with economics, assumes rational actors. If someone is crazy, then you can’t use traditional game theory on them. Who knows what they’ll do?

If you assume we are in a surveillance society, where everything you do is monitored, pretty much the only thing I can think of that you can do to protect your identity is to lie.

By this I mean, monitoring etc assumes you are a rational actor–that everything you’re doing makes sense. If I call a Pizza Hut, then I probably like pizza. If I “like” the NRA, then I’m probably a conservative.

Arguably (given what’s been discussed this year so far), you can’t protect this information. If your phone calls are being logged, then people can see that you called the Pizza Hut. There are many steps you can take to try to keep people from getting this information in the first place, but I’d argue you really can’t keep this type of information safe.

On the other hand, you can affect how reliable this information is. Right now, if 100% of your likes on Facebook represent what you like, then advertisers/others can trust your likes to build a representation of you.

However, what if 20% of your likes were totally ridiculous? E.g. liking leather shoes along with PETA, or liking the NRA and the ACLU, or whatever is against your typical personality type. Or, what if some of your calls were to your local union as well as to your small business agency? Basically, what if some of your actions were irrational?

In other words, there is another dimension of security that people don’t necessarily think about. If you increase the cost of verifying that the collected data is accurate, you introduce a new dimension of security.

(Note: I am not a lawyer and you need to read the terms of service etc for any services you use to determine if this breaks any rules etc.)


15
Aug 13

Social media sites chosen like music, not like cell phones

Yesterday I read this article, “I’m 13 and None of My Friends Use Facebook.” There have been many of its ilk. I’m wondering though if people select social media sites less like how you choose a cell phone, and more like how you choose music.

Conversations about social network providers all seem to be about which One True Service is the correct service for all users, very much like cell phones: you like iPhones, Android phones, or a couple of other types–but you are going to be on a recent version of one of these types. Very few people stick with their old vendors–they migrate (&/or are forced to migrate) to the latest options. With this thinking, a new social media site could usurp Facebook immediately–its fate determined like Blackberry’s (sorry Blackberry).

But what if this is the wrong mode of thinking for social media? Think about how people choose the music they like. The music you like is an extremely personal decision, and people tend to be very caught up in identifying themselves by the music they like. Your friends are the other people who also like They Might Be Giants, for example (holla!). We may stick with the bands we like from a long time ago, regardless of what they’re doing now.

Social media selection, like music selection, could also be generational. Lauren was telling me about a study showing that people tend to listen for their whole life to the music they listened to in their teens. Maybe the people who identify with Facebook right now will always use it, but the next generation will use a different site (or set of sites). And their identity may be caught up in the service(s) they use being different than their parents’ site.

Is Facebook like the Beatles (and Google+ either like Elvis, or the Monkees, depending on your bias)? We are at such an early stage of social media, just as how Rock and Roll was at an early stage of development with the Beatles, that Facebook like the Beatles could have swept away a generation.

I have to think that as new generations take for granted the platforms we have, it will be more and more likely for them to use have a large number of social media profiles the same way that younger people tend to be on many chat services at the same time. Then services (a la Adium/Pidgin/HootSuite) will exist to let you bulk-manage profiles. Services will need to enable automated APIs to attract users, rather than disabling them to keep from losing users.